JSON - jq

# Displaying nested keys:

This is highly useful when dealing when large JSON structure which can contain multiple nested keys. For instance, if you wanted to know what set of JSON keys lead to a value of 'XYZ', the below command would provide the keys in the format A.B.C=XYZ Note: You may encounter integers 0,1,2,etc. This basically indicates the presence of multiple values for the key i.e. Arrays "[]".

jq -r 'paths(scalars | true) as $p  | [ ( [ $p[] | tostring ] | join(".") ), ( getpath($p) | tojson )] | join(": ")'  > json.paths
Sample Output
glossary.title: "example glossary"
glossary.GlossDiv.title: "S"
glossary.GlossDiv.GlossList.GlossEntry.ID: "SGML"
glossary.GlossDiv.GlossList.GlossEntry.SortAs: "SGML"
glossary.GlossDiv.GlossList.GlossEntry.GlossTerm: "Standard Generalized Markup Language"
glossary.GlossDiv.GlossList.GlossEntry.Acronym: "SGML"
glossary.GlossDiv.GlossList.GlossEntry.Abbrev: "ISO 8879:1986"
glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para: "A meta-markup language, used to create markup languages such as DocBook."
glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso.0: "GML"
glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso.1: "XML"
glossary.GlossDiv.GlossList.GlossEntry.GlossSee: "markup"

Last updated