Resources
- Try jq online: https://jqplay.org/#
- Interactive JSON filter : https://github.com/ynqa/jnv
Basis
Usage
cat myFile.json | jq
jq myFile.json
jq <<< { "player": { "name": "apple", "color": "blue" } }
Flags
-r
Output raw strings (without the quotes).
Filter
Filter JSON Array
["myPlayer1", "myPlayer2", "myPlayer3"]
jq '.[]' myFile.json
Return elements line by line (without[ ]).jq '.[2]' myFile.json
Return an element by its ID.jq '.[2:12]' myFile.json
Return a subset of elements.
Filter JSON Object
{
"player": {
"name": "myPlayer",
"color": "blue"
}
}
jq '.player.name' myFile.json
Return the a property.jq '.player | {name,color}' myFile.json
Return the multiple properties.
jq '.player | select(.player_class == "knight")' myFile.json
Returnplayerthat have the propertyplayer_classset toknight.
jq '.player | keys' myFile.json
Return the keys ofplayer(nameandcolor).jq '.player | length' myFile.json
Return the length ofplayer(here the number of properties is 2).
