jq for JSON

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 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:

// 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!

  • Creating Scrolling Parallax Effects with CSS

    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called “parallax” effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a…

  • CSS vs. JS Animation: Which is Faster?

    CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point…

Source link

What do you think?


Leave a Reply

Your email address will not be published. Required fields are marked *

4 × three =


Food Shortages in the States

Food Shortages in the States

Video Friday: Robot Gecko Smashes Face Into Tree

Video Friday: Robot Gecko Smashes Face Into Tree