J'ai un fichier de données JSON (comme indiqué ci-dessous) et j'essaie de trouver des valeurs de champ.Analyser ou afficher les champs de données JSON à l'aide de l'utilitaire JQ où les noms de champs ont un "-" tiret dans le nom de clé
Cela fonctionne correctement sauf pour les champs si le nom de la clé contient un caractère tiret -
.
Comment puis-je obtenir les valeurs de "champ 2", "champ trois" ou "champ three.url" pour l'élément sous content.book1
(en utilisant jq
au moins)?
J'ai essayé ce qui suit pour obtenir les valeurs, mais il me donne les erreurs suivantes pour les champs dont le nom de clé contient un tiret -
dans son nom. J'ai essayé de reculer le caractère -
mais cela n'a pas aidé non plus.
types d'erreur trouvés:
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:
Commandes:
$ 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: Je sais déjà egrep
de sorte que ce n'est pas ce que je cherche.
cat /tmp/my.data.json| jq ".content.book1"|egrep "short-url|field-2"
"field-2": "value-2",
"short-url": "book1/field-three/chota-chetan"
et quelqu'un a vraiment fait un excellent travail ici: https://jqplay.org/
a ouvert un problème si elle est valide: https://github.com/stedolan/jq/issues/1464 –
Cette question a été posée, par exemple à https://stackoverflow.com/questions/37344329 – peak
Copie possible de [jq ne fonctionne pas sur le nom de l'étiquette avec des tirets] (https://stackoverflow.com/questions/37344329/jq-not-working-on-tag-name -with-dashes) – peak