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/
ein Problem Eröffnet wenn es gilt: https://github.com/stedolan/jq/issues/1464 –
Diese Frage vor gefragt wurde, z.B https://stackoverflow.com/questions/37344329 – peak
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