Getting JSON value from cURL in Linux Bash

17,392

Much more easily and reliably done using jq or jsawk:

content=$(curl ...)
package_name=$(jq -r '.package_name' <<<"$content")
Share:
17,392
Kousha
Author by

Kousha

Biomedical Engineering Master student at UBC. I develop apps for both fun and large scale application for clients.

Updated on June 07, 2022

Comments

  • Kousha
    Kousha almost 2 years

    I want to GET some json data from a server. I do this using:

    UPDATE=$(curl -i -H "Accept: application/json" -H "Content-Type: application/json" --cookie "${COOKIE_NAME}" "${1}/update/${DEVICE_NAME}");
    

    Before this, the server is authenticated. The ${1} is the server domain, ${DEVICE_NAME} is the name of the device requesting the update.

    This returns a JSON as follows:

    [{"_id":"54ff35887d8ef574029b9166","user":"54fe4313883bcec2c0ac0d64","__v":0,"created":"2015-03-10T18:18:48.023Z","status":"available","pbo_udid":"lemaker","installation_script":"","description":"Prints hello world to console","package_name":"helloworld_1.0-1.deb","name":"Hello World V1"}]

    I want to now do 2 things:

    1. Make sure data is returned (if no update is available, the server returns []
    2. Extract data, for instance package_name

    How do I do these in Linux bash script?