There are various CLI tools which could be used to query, transform JSON data.
Tool | Used by |
---|---|
jp |
AZ CLI |
jq |
GitHub CLI |
Command line JSON processor
Use an example JSON file name-age.json
# first element
jq '.[0]' ./data/name-age.json
# last element
jq '.[-1]' ./data/name-age.json
# index range
jq '.[1:3]' ./data/name-age.json
# single field of an object
jq '.[1].full_name' ./data/name-age.json
# multiple fields of an object
jq '.[1] | .full_name, .age' ./data/name-age.json
# a subset of items and fields
jq '.[1:3] | .[] | { Name:.full_name, Age:.age }' ./data/name-age.json
# {
# "Name": "Jane Smith",
# "Age": 25
# }
# {
# "Name": "Michael Johnson",
# "Age": 35
# }
# get object keys
jq '.[1] | keys[]' ./data/name-age.json
# "age"
# "country"
# "full_name"
# "gender"
.
copies input to output, and format it
echo '{"name": {"first": "Gary", "last": "Li"}}' | jq .
# {
# "name": {
# "first": "Gary",
# "last": "Li"
# }
# }
# filter by fields
jq '.[] | select(.age < 30 and .country == "Australia")' ./data/name-age.json
Use jp
(https://github.com/jmespath/jp) on command line to try out expressions.
-
Wrap string literals with single quotes
jp -f x.json "people[? contains(name, 'Barney')]"
-
Wrap number literals with backticks :
jp -f x.json "[? age==`27`]"
-
String comparing functions are case-sensitive, and seems there are no regular expression functions
{
"name": "Fred",
"age": 28,
"color": "red"
}
# single property
jp -c -f temp.json "name"
"Fred"
# multiple properties
jp -c -f temp.json "[name, color]"
["Fred","red"]
# rename multiple properties
jp -c -f temp.json "{A:name, B:color}"
{"A":"Fred","B":"red"}
Given an example JSON data like this:
{
"people": [
{
"name": "Fred",
"age": 28
},
{
"name": "Barney",
"age": 25
},
{
"name": "Wilma",
"age": 27
}
]
}
Some common expressions:
jp -c -f temp.json "people[1]"
# {"age":25,"name":"Barney"}
jp -c -f temp.json "people[1].name"
# "Barney"
jp -c -f temp.json "people[1:3].name"
# ["Barney","Wilma"]
jp -c -f temp.json "people[].name"
# ["Fred","Barney","Wilma"]
jp -c -f temp.json 'people[? age >= `27`].name'
# ["Fred","Wilma"]
jp -c -f temp.json "people[:1].{N: name, A: age}"
# [{"A":28,"N":"Fred"}]
[
{
"name": "Fred",
"age": 28
},
{
"name": "Barney",
"age": 25
},
{
"name": "Wilma",
"age": 27
}
]
Some common operations
jp -c -f temp.json "[?name == 'Fred']"
# [{"age":28,"name":"Fred"}]
jp -c -f temp.json "[?contains(name, 'F')]"
# [{"age":28,"name":"Fred"}]
jp -c -f temp.json "[? starts_with(name, 'F') || starts_with(name, 'B')].name"
# ["Fred","Barney"]
jp -c -f temp.json "[? starts_with(name, 'F') || starts_with(name, 'B')].name | sort(@) | {names: @}"
# {"names":["Barney","Fred"]}