Json: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== References == |
== References == |
||
* [https://www.json.org/ JSON.org] |
|||
* [http://seriot.ch/parsing_json.php Parsing JSON is a Minefield] |
* [http://seriot.ch/parsing_json.php Parsing JSON is a Minefield] |
||
;Tools |
|||
* [https://ravilabio.info/notes/unix/jq.html jq] — a command-line tool for manipulating JSON. |
|||
:* [https://nigeltao.github.io/blog/2020/jsonptr.html jsonptr] — same, but extra speed and safety. |
|||
* [https://github.com/tidwall/jj jj] — JSON Stream Editor (command line utility). |
|||
== Specification == |
|||
{| |
{| |
||
|- |
|- |
||
|width=20%| |
|||
| |
|||
<source lang="text"> |
<source lang="text"> |
||
object |
object |
||
Line 21: | Line 28: | ||
value , elements |
value , elements |
||
</source> |
</source> |
||
|width=20%| |
|||
| |
|||
<source lang="text"> |
<source lang="text"> |
||
value |
value |
||
Line 31: | Line 38: | ||
false |
false |
||
null |
null |
||
string |
string |
||
"" |
"" |
||
Line 39: | Line 45: | ||
char chars |
char chars |
||
</source> |
</source> |
||
|width=20%| |
|||
| |
|||
<source lang="text"> |
<source lang="text"> |
||
char |
char |
||
Line 54: | Line 60: | ||
\t |
\t |
||
\u four-hex-digits |
\u four-hex-digits |
||
⚫ | |||
|width=20%| |
|||
⚫ | |||
number |
number |
||
int |
int |
||
Line 59: | Line 68: | ||
int exp |
int exp |
||
int frac exp |
int frac exp |
||
⚫ | |||
| |
|||
⚫ | |||
int |
int |
||
digit |
digit |
||
Line 69: | Line 75: | ||
frac |
frac |
||
. digits |
. digits |
||
</source> |
|||
|width=20%| |
|||
<source lang="text"> |
|||
exp |
exp |
||
e digits |
e digits |
||
Line 89: | Line 98: | ||
{ "name" : "my_beautiful_project" } |
{ "name" : "my_beautiful_project" } |
||
</source> |
</source> |
||
== Tips == |
|||
=== Convert CSV to JSON === |
|||
<source lang="bash"> |
|||
jq -R -r 'split(",") | {name:.[0],age:.[1]}' file.csv |
|||
</source> |
|||
== Query JSON with [https://ravilabio.info/notes/unix/jq.html jq] == |
|||
Let's consider this sample JSON: |
|||
<source lang="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" |
|||
} |
|||
] |
|||
} |
|||
] |
|||
} |
|||
] |
|||
</source> |
|||
* Query the <code>lines</code> of <code>windows</code> with <code>title == "two"</code> |
|||
<source lang="bash"> |
|||
# Note the anonymous root array |
|||
jq '.[].tabs[].windows[] | select( .title == "two" ) | .lines' kitty.json |
|||
</source> |
|||
== Query JSON with Python == |
|||
See [[Python]]. |
Latest revision as of 11:04, 25 July 2023
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
ofwindows
withtitle == "two"
# Note the anonymous root array
jq '.[].tabs[].windows[] | select( .title == "two" ) | .lines' kitty.json
Query JSON with Python
See Python.