JSON - Jq Command line processor

1 - About

jq is a command line json parser.

Data in jq is represented as streams of JSON values.

3 - Management

3.1 - Get / Reference

3.1.1 - by name

  • produces the value at the properties key “foo”

jq .["foo$"] # The full qualified name
jq '.foo' # Short name

  • or

.foo|.bar  (.foo.bar is equivalent)

  • Optional (no error)

jq '.foo?'  

3.1.2 - by key

Arrays are zero-based


.[1] # returns the second element.

  • Slice 10 included, 15 excluded

.[10:15]

  • No Error

.[]?

3.1.3 - by Alias

  • as - An Json object can be saved for later use with the as keyword in a pipe

jq '. as $alias | $alias '

3.2 - Function

3.2.1 - Map (To Array)

Map(x) will run the input filter x for each element of the input array, and return the outputs in a new array

Example: The Map function returns the value of Json objects array in an array


[
    {"name":"JSON", "good":true}, 
    {"name":"XML", "good":false}
]

  • Process

jq 'map(.name)' input.json


[
  "JSON",
  "XML"
]

3.2.2 - To list

  • Create a list

[
    {"name":"JSON", "good":true}, 
    {"name":"XML", "good":false}
]

  • Process

jq '.[] | .name, .good' input.json


"JSON"
true
"XML"
false

3.2.3 - Pipe

The | operator combines two filters by feeding the output(s) of the one on the left into the input of the one on the right.


[
    {"name":"JSON", "good":true}, 
    {"name":"XML", "good":false}
]

  • Process

jq '.[] | .name' input.json


"JSON"
"XML"

3.2.4 - Join (From Array to String)

From an array


[
  "1",
  "2"
]


join (",")


"1,2"

3.3 - grouping operator


jq '(. + 2) * 5'


input: 1 output 15

4 - Get keys

4.1 - Keys

Keys Returns:

  • the index of an array
  • or the keys of an object

keys

4.2 - To Entry

  • In an array of object, get the keys by pivoting a key to a value with the to_entries

cat myJson.json | jq '."foo" | to_entries[] | .key

4.3 - To Array from Keys

  • From an array of object, returns the keys in an array

[
  { key1:Hello;key2:Nico },
  { key1:Hello;key2:Moi }
]


jq 'map(keys) | add | unique' input.json

  • Return

[
  key1,
  key2
]

5 - Output

5.1 - Raw string


jq -r # To get the result as a raw string

5.2 - Csv

Don't forget the -r options

5.2.1 - Without headers


[
    {"name":"JSON", "good":true}, 
    {"name":"XML", "good":false}
]

  • Process (@csv need an array of an array as input)

jq -r '.[] | [.name, .good] | @csv' input.json


"JSON",true
"XML",false

6 - With headers


jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'

See https://stackoverflow.com/questions/32960857/how-to-convert-arbirtrary-simple-json-to-csv-using-jq


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap