DEV Community

Cover image for How to handle JSON on the command line
Abdessamad Bensaad
Abdessamad Bensaad

Posted on

How to handle JSON on the command line

Some tool for working with json on the command line would be handy when debugging REST APIs. You can make a request, receive a response and immediately parse it, highlighting the necessary objects and fields.

Consider a command line json utility called jq that does exactly what we want. With its help, you can parse, filter, compare and transform json data effortlessly.

To install on Linux, you can simply download the binaries (there are 32 and 64 bit versions):


$ wget http://stedolan.github.io/jq/download/linux32/jq (32-bit)

$ wget http://stedolan.github.io/jq/download/linux64/jq (64-bit)

$ chmod +x ./jq

$ sudo cp jq /usr/bin

Enter fullscreen mode Exit fullscreen mode

There are also builds for Windows and Mac OS X. The complete source code is licensed under the MIT license .

Let's look at some examples. Let's say we have the following json schema.




$ cat books.json



{

"type": "examples",

"intro": {

"catgoery": "tech"

},

"books": [

{

"isbn": "9781593275846",

"title": "Eloquent JavaScript, Second Edition",

"subtitle": "A Modern Introduction to Programming",

"author": "Marijn Haverbeke",

"published": "2014-12-14T00:00:00.000Z",

"publisher": "No Starch Press",

"pages": 472,

"description": "JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications.",

"website": "http://eloquentjavascript.net/"

},

{

"isbn": "9781449331818",

"title": "Learning JavaScript Design Patterns",

"subtitle": "A JavaScript and jQuery Developer's Guide",

"author": "Addy Osmani",

"published": "2012-07-01T00:00:00.000Z",

"publisher": "O'Reilly Media",

"pages": 254,

"description": "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you.",

"website": "http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/"

},

{

"isbn": "9781449365035",

"title": "Speaking JavaScript",

"subtitle": "An In-Depth Guide for Programmers",

"author": "Axel Rauschmayer",

"published": "2014-02-01T00:00:00.000Z",

"publisher": "O'Reilly Media",

"pages": 460,

"description": "Like it or not, JavaScript is everywhere these days-from browser to server to mobile-and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position.",

"website": "http://speakingjs.com/"

},

{

"isbn": "9781491950296",

"title": "Programming JavaScript Applications",

"subtitle": "Robust Web Architecture with Node, HTML5, and Modern JS Libraries",

"author": "Eric Elliott",

"published": "2014-07-01T00:00:00.000Z",

"publisher": "O'Reilly Media",

"pages": 254,

"description": "Take advantage of JavaScript's power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that's easier-yes, easier-to work with as your code base grows.",

"website": "http://chimera.labs.oreilly.com/books/1234000000262/index.html"

}

]

}



Enter fullscreen mode Exit fullscreen mode

We get a simple field:


$ cat books.json | jq '.type'

"examples"

Enter fullscreen mode Exit fullscreen mode

Now it's a little more complicated, with nesting:


$ cat books.json | jq '.intro.catgoery'

"tech"

Enter fullscreen mode Exit fullscreen mode

For arrays:


$ cat json.txt | jq '.books[1].title'

"Learning JavaScript Design Patterns"

Enter fullscreen mode Exit fullscreen mode

Fetching a specific list of fields:


$ cat json.txt | jq '.books[1] | {author, pages}'

{

"author": "Addy Osmani",

"pages": 254

}

Enter fullscreen mode Exit fullscreen mode

you can check the documentation for more information

Top comments (0)