Reputation: 71
I need to parse a Json file which have a lot of arrays.
This is the json source:
{
"iabVersion": "IAB_V2",
"categories": [{
"categories": [{
"categories": [{
"id": "1.1.1",
"name": "Commercial Trucks"
},
{
"id": "1.1.2",
"name": "Convertible"
},
{
"id": "1.1.3",
"name": "Coupe"
},
{
"id": "1.1.4",
"name": "Crossover"
},
{
"id": "1.1.5",
"name": "Hatchback"
},
{
"id": "1.1.6",
"name": "Microcar"
},
{
"id": "1.1.7",
"name": "Minivan"
},
{
"id": "1.1.8",
"name": "Off-Road Vehicles"
},
{
"id": "1.1.9",
"name": "Pickup Trucks"
},
{
"id": "1.1.10",
"name": "Sedan"
},
{
"id": "1.1.11",
"name": "Station Wagon"
},
{
"id": "1.1.12",
"name": "SUV"
},
{
"id": "1.1.13",
"name": "Van"
}],
"id": "1.1",
"name": "Auto Body Styles"
}
}
}
This is the json requred:
{
"id": "1.1.1",
"name": "Commercial Trucks"
}
{
"id": "1.1.2",
"name": "Convertible"
}
How can I parse it via jq?
10x:)
Upvotes: 4
Views: 35451
Reputation: 116730
Assuming the JSON input has been corrected, the following jq filter seems to meet the requirements, such as they are:
.categories[].categories[].categories[]
This produces a stream of JSON objects, beginning:
{
"id": "1.1.1",
"name": "Commercial Trucks"
}
{
"id": "1.1.2",
"name": "Convertible"
}
Upvotes: 8
Reputation: 13249
Given that you fix the malformed JSON data by closing arrays like this:
...
"id": "1.1",
"name": "Auto Body Styles"
}
]
}
]
}
you can use the following jq
script:
$ jq '.categories[0].categories[0].categories[0],.categories[0].categories[0].categories[1]' file
{
"id": "1.1.1",
"name": "Commercial Trucks"
}
{
"id": "1.1.2",
"name": "Convertible"
}
The jq statement selects the first and second array element in these nested arrays.
Upvotes: 2