4

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

3
  • your output has only 2 objects, do you really need only the first 2 items? Commented May 9, 2018 at 11:36
  • @user9763887 - please clarify the functional requirements. Commented May 9, 2018 at 12:19
  • I need the whole objects, the 2 object are just example to how the output should look like. even if I run : cat /home/ubuntu/pcn/iab/source/dmp-data-source.json | jq '.categories[0].categories[0].categories[],.categories[0].categories[0].categories[]' > /home/ubuntu/pcn/iab/result/dmp-data-result1.json It did not catch the "id"="1.1" Commented May 9, 2018 at 13:17

2 Answers 2

8

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"
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

But the Json as required need to fill in all the id+name (not just in the example)...
If I understand you well, you need jq '.categories[0].categories[0].categories[]' file. If not, please update your question with the exact requirement.
I need the whole objects, the 2 object are just example to how the output should look like. even if I run : cat /home/ubuntu/pcn/iab/source/dmp-data-source.json | jq '.categories[0].categories[0].categories[],.categories[0].categories[0].categories[]' > /home/ubuntu/pcn/iab/result/dmp-data-result1.json It did not catch the "id"="1.1"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.