jq for JSON

I old enough to remember when we thought XML was going to change the programming world…then JSON saved us from that hell. Parsing and querying JSON data is fundamental task we’ve all coded for, but sometimes I just want to get some data locally with minimal fuss. I just learned of a really awesome library […]

The post jq for JSON appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh

I old enough to remember when we thought XML was going to change the programming world…then JSON saved us from that hell. Parsing and querying JSON data is fundamental task we’ve all coded for, but sometimes I just want to get some data locally with minimal fuss. I just learned of a really awesome library to do so: jq. Let’s have a look at some cool things we can do with jq!

Start by installing jq via a utility like Homebrew:

brew install jq

With Homebrew installed and a local actors.json file, let’s go to work on pulling some data!

// Using this JSON file:
// https://raw.githubusercontent.com/algolia/datasets/master/movies/actors.json

// Get the 10th item in an array
cat actors.json | jq '.[10]'
// {
//   "name": "Dwayne Johnson",
//   "rating": 1568,
//   "image_path": "/akweMz59qsSoPUJYe7QpjAc2rQp.jpg",
//   "alternative_name": "The Rock",
//   "objectID": "551486400"
// }

// Get a property from the 10th item in array
// > "Dwayne Johnson"

// Get multiple items
jq '.[10:12]'

// Get items up to the 12th position
jq '.[:12]'

// Get items after the 12th position
jq '.[12:]'

// Get an array of properties from all objects
jq '.[].name'
// > ["William Shatner", "Will Ferrell", ...]

// Create an object with only properties I want
jq '{ name: .[].name, rating: .[].rating}'

// Built in functions!
jq 'sort'
jq 'length'
jq 'reverse'

There are loads of other ways to use jq, so I highly recommend you check out JQ Select Explained: Selecting elements from JSON. I’ll keep jq handy for the foreseeable future, as it will be invaluable!

The post jq for JSON appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh


Print Share Comment Cite Upload Translate Updates
APA

David Walsh | Sciencx (2021-08-27T16:29:41+00:00) jq for JSON. Retrieved from https://www.scien.cx/2021/08/27/jq-for-json/

MLA
" » jq for JSON." David Walsh | Sciencx - Friday August 27, 2021, https://www.scien.cx/2021/08/27/jq-for-json/
HARVARD
David Walsh | Sciencx Friday August 27, 2021 » jq for JSON., viewed ,<https://www.scien.cx/2021/08/27/jq-for-json/>
VANCOUVER
David Walsh | Sciencx - » jq for JSON. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/27/jq-for-json/
CHICAGO
" » jq for JSON." David Walsh | Sciencx - Accessed . https://www.scien.cx/2021/08/27/jq-for-json/
IEEE
" » jq for JSON." David Walsh | Sciencx [Online]. Available: https://www.scien.cx/2021/08/27/jq-for-json/. [Accessed: ]
rf:citation
» jq for JSON | David Walsh | Sciencx | https://www.scien.cx/2021/08/27/jq-for-json/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.