Issue
Let say I have the following JSON:
{
"MS-23-AMW": {
"brand": "audi",
"model": "A8",
"displayName": "Audi A8",
"engine": "4.5L"
},
"SJ-08-REW": {
"brand": "bmw",
"model": "335i",
"displayName": "bmw 3 series",
"engine": "3.5L"
}
}
How could I get the model lets say for MS-23-AMV
with JQ?
I tried jq "cars.MS-23-AMW" cars.json
but i get jq: error: syntax error, unexpected LITERAL, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
. Any ideas?
Solution
I think the array of cars should be defined as an object where each car is a key-value pair, or as an array of objects
something like this
{
"cars": {
"ms-23-amw": {
"brand": "audi",
"model": "a8",
"displayname": "audi a8",
"engine": "4.5l"
},
"sj-08-rew": {
"brand": "bmw",
"model": "335i",
"displayname": "bmw 3 series",
"engine": "3.5l"
}
}
}
and now use the jq
to get the model for ms-23-amw
:
jq '.cars."ms-23-amw".model' cars.json
output
"a8"
Answered By - Freeman Answer Checked By - Mary Flores (WPSolving Volunteer)