2017-08-16 2 views
-1

Ich habe eine JSON-Datei (wie unten gezeigt) und ich versuche, Feldwerte mit jq Dienstprogramm zu finden.Analysieren oder Anzeigen von JSON-Datenfeldern mit JQ-Tool-Dienstprogramm Feldnamen haben einen "-" Strich im Schlüsselnamen

Es funktioniert gut außer für Felder, wenn der Schlüsselname ein - Bindestrich enthält.

Wie kann ich die Werte von „Feld 2“, „Feld drei“ oder „Feld three.url“ für Element unter content.book1 (mit jq mindestens)?

Ich habe Folgendes versucht, um die Werte zu erhalten, aber es gibt mir die folgenden Fehler für Felder, deren Schlüsselname einen Strich - in seinem Namen enthält. Ich habe versucht, - Zeichen zurückzuschneiden, aber das half auch nicht.

Fehlertypen gefunden:

jq: error (at <stdin>:27): null (null) and number (2) cannot be subtracted 
jq: 1 compile error 

jq: error: three/0 is not defined at <top-level> 

jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level> 

jq: error: syntax error, unexpected INVALID_CHARACTER, expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1: 

Befehle:

$ cat /tmp/my.data.json 
{ 
    "pages": { 
    "book1": [ 
     "page1", 
     "page2-para1", 
     "page3-para1-sentence1", 
     "page3-para2-sentence3-word4" 
    ] 
    }, 
    "content": { 
    "book1": { 
     "name": "giga", 
     "url": "-", 
     "field1": "value1", 
     "field-2": "value-2", 
     "field-three": { 
     "name": "THIRD", 
     "url": "book1/field-three/", 
     "short-url": "book1/field-three/chota-chetan" 
     }, 
     "authur": { 
     "name": "lori CHUCK", 
     "displayIndex": 4 
     }, 
     "route": "/in-gc/hindi-chini-bhai-bhai" 
    } 
    } 
} 

$ cat /tmp/my.data.json| jq ".pages" 
{ 
    "book1": [ 
    "page1", 
    "page2-para1", 
    "page3-para1-sentence1", 
    "page3-para2-sentence3-word4" 
    ] 
} 

$ cat /tmp/my.data.json| jq ".pages.book1[0]" 
"page1" 

$ cat /tmp/my.data.json| jq ".pages.book1[1]" 
"page2-para1" 

$ cat /tmp/my.data.json| jq ".content" 
{ 
    "book1": { 
    "name": "giga", 
    "url": "-", 
    "field1": "value1", 
    "field-2": "value-2", 
    "field-three": { 
     "name": "THIRD", 
     "url": "book1/field-three/" 
    }, 
    "authur": { 
     "name": "lori CHUCK", 
     "displayIndex": 4 
    }, 
    "route": "/in-gc/hindi-chini-bhai-bhai" 
    } 
} 

$ cat /tmp/my.data.json| jq ".content.book1" 
{ 
    "name": "giga", 
    "url": "-", 
    "field1": "value1", 
    "field-2": "value-2", 
    "field-three": { 
    "name": "THIRD", 
    "url": "book1/field-three/" 
    }, 
    "authur": { 
    "name": "lori CHUCK", 
    "displayIndex": 4 
    }, 
    "route": "/in-gc/hindi-chini-bhai-bhai" 
} 

$ cat /tmp/my.data.json| jq ".content.book1.name" 
"giga" 

$ cat /tmp/my.data.json| jq ".content.book1.field1" 
"value1" 

$ cat /tmp/my.data.json| jq ".content.book1.field-2" 
jq: error (at <stdin>:27): null (null) and number (2) cannot be subtracted 

$ cat /tmp/my.data.json| jq ".content.book1.field-three" 
jq: error: three/0 is not defined at <top-level>, line 1: 
.content.book1.field-three 
jq: 1 compile error 

$ cat /tmp/my.data.json| jq ".content.book1.field-three.url" 
jq: error: three/0 is not defined at <top-level>, line 1: 
.content.book1.field-three.url 
jq: 1 compile error 

$ cat /tmp/my.data.json| jq ".content.book1.field\-2"  
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 1: 
.content.book1.field\-2      
jq: 1 compile error 

$ cat /tmp/my.data.json| jq ".content.book1.field\\-2" 
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 1: 
.content.book1.field\-2      
jq: 1 compile error 

$ cat /tmp/my.data.json| jq ".content.book1.'field-2'" 
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1: 
.content.book1.'field-2'    
jq: 1 compile error 

$ cat /tmp/my.data.json| jq ".content.book1.authur" 
{ 
    "name": "lori CHUCK", 
    "displayIndex": 4 
} 

$ cat /tmp/my.data.json| jq ".content.book1.route" 
"/in-gc/hindi-chini-bhai-bhai" 

$ 

PS: Ich weiß schon egrep das ist so nicht das, was ich suche.

cat /tmp/my.data.json| jq ".content.book1"|egrep "short-url|field-2" 
    "field-2": "value-2", 
    "short-url": "book1/field-three/chota-chetan" 

und jemand hat wirklich einen tollen Job hier: https://jqplay.org/

+0

ein Problem Eröffnet wenn es gilt: https://github.com/stedolan/jq/issues/1464 –

+0

Diese Frage vor gefragt wurde, z.B https://stackoverflow.com/questions/37344329 – peak

+0

Mögliches Duplikat von [jq funktioniert nicht mit Tag-Namen mit Bindestrichen] (https://stackoverflow.com/questions/37344329/jq-not-working-on-tag-name (mit Bindestrichen) – peak

Antwort

4

"-" wird für die Negation in jq verwendet. Für Schlüsselnamen mit Sonderzeichen wie "-" kann die vereinfachte Syntax ".keyname" nicht verwendet werden. Es gibt mehrere Alternativen, aber am robustesten ist es einfach, das Formular .["KEY NAME"] zu verwenden, das abgekürzt werden kann zu ["KEY NAME"], wenn es z. .a["b-c"] ist eine Kurzschrift für .a | .["b-c"].

Im Zweifelsfall die Leitung explizit verwenden.

Für weitere Informationen konsultieren Sie bitte die jq manuell und/oder https://github.com/stedolan/jq/wiki/FAQ

+0

Schön, ich werde das als Antwort akzeptieren, aber danke an alle. –

1

Ich weiß nicht, über jq, aber Sie setzen Python in den Tags:

$ cat test.json | python -c "import sys, json; print(json.load(sys.stdin)['content']['book1']['field-three']['name'])" 
THIRD 

oder ohne das Rohr :

$ python -c "import json; print(json.load(open('test.json'))['content']['book1']['field-three']['name'])" 
+0

Danke Alex; Mit Python habe ich bereits die selbe Lösung benutzt wie du gesagt hast, aber ich frage mich, was in 'jq' schlecht ist :) und warum es mir einen Fehler für solch eine einfache Operation gibt –

+0

Up Voting, da es ein guter Liner ist. –

1

Wie in der jq manual erklärt, um Handle Schlüssel mit nicht-identischen Zeichen wie - können Sie doppelte Anführungszeichen verwenden.

Von der Shell ist dies am einfachsten, wenn Sie einfache Anführungszeichen um Ihren Filter verwenden. Versuchen Sie beispielsweise die folgenden Befehle:

cat /tmp/my.data.json | jq '.pages' 
cat /tmp/my.data.json | jq '.pages.book1[0]' 
cat /tmp/my.data.json | jq '.pages.book1[1]' 
cat /tmp/my.data.json | jq '.content' 
cat /tmp/my.data.json | jq '.content.book1' 
cat /tmp/my.data.json | jq '.content.book1.name' 
cat /tmp/my.data.json | jq '.content.book1.field1' 
cat /tmp/my.data.json | jq '.content.book1."field-2"' 
cat /tmp/my.data.json | jq '.content.book1."field-three"' 
cat /tmp/my.data.json | jq '.content.book1."field-three".url' 
cat /tmp/my.data.json | jq '.content.book1.authur' 
cat /tmp/my.data.json | jq '.content.book1.route' 
+0

danke, sehr hilfreich. –

0

Hm..took einige Zeit, aber schließlich scheint es, wie wir zu doppelten Anführungszeichen brauchen es und zurück Schrägstrich nur die doppelten Anführungszeichen für jede Taste Name, der eine - in seinem Namen enthält.

$ cat /tmp/my.data.json| jq ".content.book1.\"field-2\""      
"value-2" 

$ cat /tmp/my.data.json| jq ".content.book1.\"field-three\".\"url\"" 
"book1/field-three/" 

OR , wenn Sie alles in einem einfachen Anführungszeichen ' wickeln, dann brauchen wir nicht " doppelte Anführungszeichen diesen Schrägstrich aber doppelte Anführungszeichen für Schlüsselnamen mit - in ihrem Namen zu verwenden.

$ cat /tmp/my.data.json| jq '.content.book1."field-three"."url"' 
"book1/field-three/" 

Hoffe es hilft! Nahm etwas Hilfe/Hinweis von https://jqplay.org/

Sehen Sie diese für mehr: https://github.com/stedolan/jq/issues/38#issuecomment-9770240

Verwandte Themen