Getting JSON value from cURL in Linux Bash
Much more easily and reliably done using jq or jsawk:
content=$(curl ...)
package_name=$(jq -r '.package_name' <<<"$content")
Kousha
Biomedical Engineering Master student at UBC. I develop apps for both fun and large scale application for clients.
Updated on June 07, 2022Comments
-
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:
- Make sure data is returned (if no update is available, the server returns
[]
- Extract data, for instance
package_name
How do I do these in Linux bash script?
- Make sure data is returned (if no update is available, the server returns