HomecurlcURL - how to pretty print JSON output?

cURL – how to pretty print JSON output?

In this guide we are going to see how to pretty print JSON output in cURL in command line. Unformatted JSON data may not be readable. Often we need to read curl json output on the command line. There are several command-line JSON data pretty printers available.

1. Badly Formatted JSON Output

curl https://dummyjson.com/products/1

{"id":1,"title":"iPhone 9","description":"An apple mobile which is nothing like apple","price":549,"discountPercentage":12.96,"rating":4.69,"stock":94,"brand":"Apple","category":"smartphones","thumbnail":"https://dummyjson.com/image/i/products/1/thumbnail.jpg","images":["https://dummyjson.com/image/i/products/1/1.jpg","https://dummyjson.com/image/i/products/1/2.jpg","https://dummyjson.com/image/i/products/1/3.jpg","https://dummyjson.com/image/i/products/1/4.jpg","https://dummyjson.com/image/i/products/1/thumbnail.jpg"]}

2. Most commonly used light-weight JSON pretty printers

Following are the command-line tools for JSON output pretty print. We can pipe cURL JSON output to one of the following tool to pretty print in the command line.

  1. json_ppcurl -s https://dummyjson.com/products/1 | json_pp
  2. jq curl -s https://dummyjson.com/products/1 | jq .
  3. Python json.toolcurl -s https://dummyjson.com/products/1 | python3.10 -m json.tool
  4. yajl-tools curl -s https://dummyjson.com/products/1 | json_reformat
  5. jsonlint curl -s https://dummyjson.com/products/1 | jsonlint
  6. jsontoolcurl -s https://dummyjson.com/products/1 | json

For all the above cURL commands, following is the JSON output. The order of key values are different for each tool. None of the following tools has color output feature except jq tool. jq and jsontool has JSON filtering feature additionally.

json_pp sorts the data based on keys, it pre installed in most of linux distributions.

jq has coloring, parsing and filtering features, to install in linux ubuntu use command apt-get install jq

jsontool has filtering features and no color feature. It’s a npm package. To install, use command npm install jsontool -g

jsonlint also a npm package. To install, use command npm install jsontool -g

yajl-tools (Yet Another JSON Library – Tools) – To install linux – sudo apt-get install yajl-tools

Python installation comes with json.tools module, which can be used to format JSON.

{
  "id": 1,
  "title": "iPhone 9",
  "description": "An apple mobile which is nothing like apple",
  "price": 549,
  "discountPercentage": 12.96,
  "rating": 4.69,
  "stock": 94,
  "brand": "Apple",
  "category": "smartphones",
  "thumbnail": "https://dummyjson.com/image/i/products/1/thumbnail.jpg",
  "images": [
    "https://dummyjson.com/image/i/products/1/1.jpg",
    "https://dummyjson.com/image/i/products/1/2.jpg",
    "https://dummyjson.com/image/i/products/1/3.jpg",
    "https://dummyjson.com/image/i/products/1/4.jpg",
    "https://dummyjson.com/image/i/products/1/thumbnail.jpg"
  ]
}

4. Conclusion

In this guide we have discussed how to pretty print JSON output in cURL using command line pretty printers.

5. References

  1. json — JSON encoder and decoder — Python 3.10.6 documentation
  2. json pretty-printers comparison

LEAVE A REPLY

Please enter your comment!
Please enter your name here