Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse an array of json object using jq

Tags:

json

parsing

jq

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:)

like image 591
user9763887 Avatar asked Sep 30 '25 09:09

user9763887


1 Answers

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"
}
like image 72
peak Avatar answered Oct 02 '25 06:10

peak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!