Bootstrap FreeKB - Curl - Parse JSON using Python
Curl - Parse JSON using Python

Updated:   |  Curl articles

Let's say there is an API that is configured to return JSON.

curl
--request POST 
--url https://api.example.com/foo/bar/ 
--data '{ "Username": "john.doe", "Password": "itsasecret" }' 
--header "Content-Type: application/json" 

 

Typically, the output will be truncated into one line, something like this.

[{"access_token":"abc123","refresh_token":"T2w8o3xgET2K9Dh8abqnWQ==","expires_in":31536000,"expires":1655380828,"token_type":"Bearer","refresh_until":1655380828}]

 

The jq command can be used to parse the JSON. However, you may not have jq installed. The which command can be used to determine if jq is in your $PATH.

which jq

 

If something like this is return, jq is not in your $PATH, will probably means jq is not installed on your system.

/usr/bin/which: no jq in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/john.doe/bin)

 

If jq is not installed and cannot be installed (such as in a corporate environment), python can be used to parse the JSON.

curl
--request POST 
--url https://api.example.com/foo/bar/ 
--data '{ "Username": "john.doe", "Password": "itsasecret" }' 
--header "Content-Type: application/json" | python -m json.tool

 

Something like this should be returned.

[
 {
 "access_token":"abc123",
 "refresh_token":"T2w8o3xgET2K9Dh8abqnWQ==",
 "expires_in":31536000,
 "expires":1655380828,
 "token_type":"Bearer",
 "refresh_until":1655380828
 }
]

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 99eba1 in the box below so that we can be sure you are a human.