Making a curl to an API and get an specific field from the JSON Making a curl to an API and get an specific field from the JSON shell shell

Making a curl to an API and get an specific field from the JSON


By using jq you could parse the json data instead of the text based parsing.

curl -X GET "https://api.mercadolibre.com/items/MLA511127356" | jq '.[].id'


As mentioned here you can use grep -Po '"keyThatYouWant":.*?[^\\]",' file.json. Like that:

local result=$(curl -X GET $YOUR_HUGE_URL)echo $result | grep -Po '"keyThatYouWant":.*?[^\\]",'


jq is pretty impressive but if you do not want to install another dependency, using python is a nice alternative by also giving you some extra flexibility if further processing is desired.

Jist of it is to pipe the json curl result to python -c.

Example:

curl -X GET https://jsonplaceholder.typicode.com/comments/4 | python -c "import sys,json; print json.load(sys.stdin)['email']"