Json

From miki
Jump to navigation Jump to search

References

Tools
  • jq — a command-line tool for manipulating JSON.
  • jsonptr — same, but extra speed and safety.
  • jj — JSON Stream Editor (command line utility).

Specification

object
    {}
    { members } 
members
    pair
    pair , members
pair
    string : value
array
    []
    [ elements ]
elements
    value
    value , elements
value
    string
    number
    object
    array
    true
    false
    null
string
    ""
    " chars "
chars
    char
    char chars
char
    any-Unicode-character-
        except-"-or-\-or-
        control-character
    \"
    \\
    \/
    \b
    \f
    \n
    \r
    \t
    \u four-hex-digits
number
    int
    int frac
    int exp
    int frac exp 
int
    digit
    digit1-9 digits
    - digit
    - digit1-9 digits 
frac
    . digits
exp
    e digits
digits
    digit
    digit digits
e
    e
    e+
    e-
    E
    E+
    E-

JSON query

Example of query:

{ "name" : "my_beautiful_project" }

Tips

Convert CSV to JSON

jq -R -r 'split(",") | {name:.[0],age:.[1]}' file.csv

Query JSON with jq

Let's consider this sample JSON:

[
   {
   "tabs": [
     {
       "title": "foo",
       "windows": [
         {
           "columns": 137,
           "title": "first",
           "lines": 74,
         }
       ]
     },
     {
       "title": "bar",
       "windows": [
         {
           "columns": 68,
           "lines": 74,
           "title": "one"
         },
         {
           "columns": 67,
           "lines": 37,
           "title": "two"
         },
         {
           "columns": 67,
           "lines": 37,
           "title": "three"
         }
       ]
     }
   ]
   }
]
  • Query the lines of windows with title == "two"
# Note the anonymous root array
jq '.[].tabs[].windows[] | select( .title == "two" ) | .lines' kitty.json

Query JSON with Python

See Python.