1. Home
  2. Tips and Tricks
  3. Parse JSON Responses using Bash Scripts

Parse JSON Responses using Bash Scripts

I was working with the categorify.org site and I wanted to parse through the API’s response. The response was in JSON format.

There are a number of different ways you can do this, but if you want a quick, simple, way that uses existing tools you probably already have installed, this is for you.

To parse through the JSON response I used Python.

Here is an example of what I was doing:

curl -s https://categorify.org/api?website=pornhub.com

If you do this, the response is something like this:

{"domain":"pornhub.com","ip":"","country-code":"NL","country":"Netherlands", "rating":{"language":true,"violence":false,"nudity":true,"adult":true,"value":"R & NSFW", "description":"Adult-only content and not safe for a work environment"},"category": ["Adult\Pornography"],"keyword_heatmap" {"videos":99,"free":85,"pornhub":61,"porn":55, "models":54,"premium":45,"content":45,"rated":44,"photos":44,"exclusive":43,"party":43, "gifs":42,"sister":39,"discover":36,"online":36,"video":35,"albums":34,"pornhubcom":33, "cancel":33,"subscribed":30}}

What I specifically wanted was the category value: “category”:[“Adult\/Pornography”]

So I modified my command to pipe my output to python and ran the following:

$ curl -s https://categorify.org/api?website=pornhub.com | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["category"]'

The section you’re most interested in is this: print obj[“category”]

You have to choose which object you want to print. In my case, I wanted the category object as it holds the value I am looking for.

You can use this to parse any JSON response, just update the print object with one that corresponds to your results. The beautiful part is that this should be available on most major OS platforms without additional installations.

Side note, here are two variations depending on which version of Python you have on your machine.

Python 3

python3 -c "import sys, json; print(json.load(sys.stdin)['category'])"

Python 2

python2 -c "import sys, json; print json.load(sys.stdin)['category']"

If you simply want a pretty display of the json you can pipe the output into “| python3 -m json.tool”. This will give you something much easier to read:

$ curl -s https://categorify.org/api?website=pornhub.com | python3 -m json.tool 
"domain": "pornhub.com",
"ip": "",
"country-code": "NL",
"country": "Netherlands",
"rating": {
"language": true,
"violence": false,
"nudity": true,
"adult": true,                                                                                                                                                    
"value": "R & NSFW",                                                                                                                                              
"description": "Adult-only content and not safe for a work environment"                                                                                       
"category": [                                                                                                                                                         
"keyword_heatmap": {                                                                                                                                                  
"videos": 99,                                                                                                                                                     
"free": 85,                                                                                                                                                       
"pornhub": 61,                                                                                                                                                    
"porn": 55,                                                                                                                                                       
"models": 54,                                                                                                                                                     
"premium": 45,                                                                                                                                                    
"content": 45,                                                                                                                                                   
 "rated": 44,                                                                                                                                                      
 "photos": 44,                                                                                                                                                     
 "exclusive": 43,                                                                                                                                                  
 "party": 43,                                                                                                                                                      
 "gifs": 42,                                                                                                                                                       
 "sister": 39,                                                                                                                                                     
 "discover": 36,                                                                                                                                                   
 "online": 36,                                                                                                                                                     
 "video": 35,                                                                                                                                                      
 "albums": 34,                                                                                                                                                     
 "pornhubcom": 33,                                                                                                                                                 
 "cancel": 33,                                                                                                                                                     
 "subscribed": 30                                                                                                                                              
Updated on December 8, 2023

Was this article helpful?

Related Articles

Need Support?
Can’t find the answer you’re looking for? Don’t worry we’re here to help!
Email: support@noc.org

Leave a Comment