mojoa
mojoa

Reputation: 123

How to print path and key values of JSON file using JQ

I would like to print each path and value of a file with included key values line by line. I would like the output to be comma delimited or at least very easy to cut and sort using Linux command line tools. Given the following and , I have been given code which seems to do this for the test JSON, but I am not sure it works in all cases or is the proper approach.

Is there a function in which does this automatically? If not, is there a "most concise best way" to do it?

My wish would be something like:

$ cat short.json | jq -doit '.'
Reservations,0,Instances,0,ImageId,ami-a
Reservations,0,Instances,0,InstanceId,i-a
Reservations,0,Instances,0,InstanceType,t2.micro
Reservations,0,Instances,0,KeyName,ubuntu


Test JSON:

$ cat short.json | jq '.'
{
  "Reservations": [
    {
      "Groups": [],
      "Instances": [
        {
          "ImageId": "ami-a",
          "InstanceId": "i-a",
          "InstanceType": "t2.micro",
          "KeyName": "ubuntu"
        }
      ]
    }
  ]
}  

Code Recommended:
https://unix.stackexchange.com/questions/561460/how-to-print-path-and-key-values-of-json-file
Supporting:
https://unix.stackexchange.com/questions/515573/convert-json-file-to-a-key-path-with-the-resulting-value-at-the-end-of-each-k
JQ Code Too long and complicated!

jq -r '
paths(scalars) as $p
  | [ ( [ $p[] | tostring ] | join(".") )
    , ( getpath($p) | tojson )
    ]
  | join(": ")
' short.json  

Result:  
Reservations.0.Instances.0.ImageId: "ami-a"
Reservations.0.Instances.0.InstanceId: "i-a"
Reservations.0.Instances.0.InstanceType: "t2.micro"
Reservations.0.Instances.0.KeyName: "ubuntu"  


Upvotes: 6

Views: 9679

Answers (3)

Banoona
Banoona

Reputation: 554

Use -c paths as follows:

cat short.json | jq -c paths | tr -d '[' | tr -d ']'

I am using jq-1.5-1-a5b5cbe

Upvotes: 0

Jeff Mercado
Jeff Mercado

Reputation: 134801

Read it as a stream.

$ jq --stream -r 'select(.[1]|scalars!=null) | "\(.[0]|join(".")): \(.[1]|tojson)"' short.json

Upvotes: 2

peak
peak

Reputation: 116650

A simple jq query to achieve the requested format:

 paths(scalars) as $p
 | $p + [getpath($p)] 
 | join(",")

If your jq is ancient and you cannot upgrade, insert | map(tostring) before the last line above.

Output with the -r option

Reservations,0,Instances,0,ImageId,ami-a
Reservations,0,Instances,0,InstanceId,i-a
Reservations,0,Instances,0,InstanceType,t2.micro
Reservations,0,Instances,0,KeyName,ubuntu

Caveat

If a key or atomic value contains "," then of course using a comma may be inadvisable. For this reason, it might be preferable to use a character such as TAB that cannot appear in a JSON key or atomic value. Consider therefore using @tsv:

paths(scalars) as $p
| $p + [getpath($p)]
| @tsv

(The comment above about ancient versions of jq applies here too.)

Upvotes: 9

Related Questions