Jeya Suriya Muthumari
Jeya Suriya Muthumari

Reputation: 2021

jq: Iterate over inner multiple array

I have the following JSON file.

{
    "@odata.context": "https://from.some.API",
    "value": [
        {
            "id": 2,
            "label": "Marketing and Public Relations",
            "parentname": [
                {
                    "id": 336,
                    "label": "Marketing Software",
                    "children": [
                        {
                            "id": 16543,                            
                            "label": "Sun Application Server"                            
                        },
                        {
                            "id": 15735,                            
                            "label": "Kenshoo"                            
                        }                        
                    ]
                }                
            ]
        },
        {
            "id": 3,
            "label": "Sales",
            "parentname": [
                {
                    "id": 115,
                    "label": "Online Sales",
                    "children": [
                        {
                            "id": 9919,                            
                            "label": "Online Sales"                            
                        },
                        {
                            "id": 9489,                            
                            "label": "Internet Sales"                            
                        }
                    ]
                },
                {
                    "id": 115,
                    "label": "Online Sales2",
                    "children": [
                        {
                            "id": 9919,                            
                            "label": "Online Sales2"                            
                        },
                        {
                            "id": 9489,                            
                            "label": "Internet Sales2"                            
                        }
                    ]
                }                
            ]
        }
    ]
}

I am trying to extract like below,

{
  "main": "Marketing and Public Relations",
  "parent": "Marketing Software",
  "children": [
    "Sun Application Server",
    "Kenshoo"
  ]
}
{
  "main": "Sales",
  "parent": "Online Sales",
  "children": [
    "Online Sales",
    "Internet Sales"
  ]
}
{
  "main": "Sales",
  "parent": "Online Sales2",
  "children": [
    "Online Sales2",
    "Internet Sales2"
  ]
}

however for the following jq query,

type Example.json|jq ".value[] | {main: .label, parent: .parentname[].label, children: [.parentname[].children[].label]}"

I am getting the results like,

{
  "main": "Marketing and Public Relations",
  "parent": "Marketing Software",
  "children": [
    "Sun Application Server",
    "Kenshoo"
  ]
}
{
  "main": "Sales",
  "parent": "Online Sales",
  "children": [
    "Online Sales",
    "Internet Sales",
    "Online Sales2",
    "Internet Sales2"
  ]
}
{
  "main": "Sales",
  "parent": "Online Sales2",
  "children": [
    "Online Sales",
    "Internet Sales",
    "Online Sales2",
    "Internet Sales2"
  ]
}

I have tried sometimes to loop the inner array only. but there are multiple inner arrays in my JSON and it is iterating over the main array only.

Any suggestion would be helpful.

Upvotes: 0

Views: 1645

Answers (2)

Dmitry
Dmitry

Reputation: 1293

here's also an alternative way of achieving the same, using a walk-path based utility jtc:

bash $ <file.json jtc -w'[value][:][label]<M>v[-1][parentname][:][label]<P>v[-1][children][0][label]<C>v[-2][1][label]' -T'{"main":{{M}},"parent":{{P}},"children":[{{C}},{{}}]}'
{
   "children": [
      "Sun Application Server",
      "Kenshoo"
   ],
   "main": "Marketing and Public Relations",
   "parent": "Marketing Software"
}
{
   "children": [
      "Online Sales",
      "Internet Sales"
   ],
   "main": "Sales",
   "parent": "Online Sales"
}
{
   "children": [
      "Online Sales2",
      "Internet Sales2"
   ],
   "main": "Sales",
   "parent": "Online Sales2"
}
bash $ 

the walk-path (-w) might look a bit bulky here, but it's just walking the source JSON collecting into respective namespaces all required values.

Once collected, the template (-T) will do its job interpolating all the namespaces and outputting the resulted JSON

PS> Disclosure: I'm the creator of the jtc - shell cli tool for JSON operations

Upvotes: 1

peak
peak

Reputation: 116800

You only need to iterate over the subarrays (.parentName) once:

.value[]
| {main: .label}
  + (.parentname[] 
     | {parent: .label, children: [ .children[].label ] }) 

Upvotes: 1

Related Questions