2

I'm currently trying to grab and assign the N_596164000673190002 to a variable from a curl command.

This is the command:

curl -L -H 'X-Cisco-Meraki-API-Key: mykeygoeshere' -X POST -H'Content-Type: application/json' --data-binary '{"name":"'"$NETWORK_NAME"'", "type":"appliance", "timeZone":"'"$TIME_ZONE"'"}' 'https://dashboard.meraki.com/api/v0/organizations/foobar/networks'

This is the response:

{"id":"N_596164000673190002","organizationId":"foo","type":"appliance","name":"bar","timeZone":"America/Chicago","tags":""}

How do I successfully read and grab the variable after id (without the double quotes), while also simutaneously assigning it to a variable, $NETWORK_ID? I imagine this can all be done in one line.

If this is successful, echo $NETWORK_ID should return N_596164000673190002

jagdpanzer
  • 693
  • 2
  • 10
  • 35
  • There are many duplicates; if you don't like the one I nominated, please clarify your requirements. For how to assign output to a variable, the canonical is http://stackoverflow.com/questions/4651437/how-to-set-a-variable-equal-to-the-output-from-a-command-in-bash – tripleee Jan 25 '17 at 04:29

2 Answers2

4

To parse json in bash, people usually use jq as it is installed by default on most Unix distributions.

Try the following :

NETWORK_ID=$(my_curl_command | jq -r '.id')

Here, '.id' is a filter indicating we want to retrieve the value for the key id, and the -r flag is used to remove double quotes from the output.

Aserre
  • 4,916
  • 5
  • 33
  • 56
1

Pipe the JSON output to python json to grab the id value you need, and use bash command substitution to assign the result to your NETWORK_ID environment variable.

NETWORK_ID=$(curl -L -H 'X-Cisco-Meraki-API-Key: mykeygoeshere' -X POST \
  -H'Content-Type: application/json' \
  --data-binary '{"name":"'"$NETWORK_NAME"'", "type":"appliance", \
  "timeZone":"'"$TIME_ZONE"'"}' \
  'https://dashboard.meraki.com/api/v0/organizations/foobar/networks' \
 | python -c "import sys, json; print json.load(sys.stdin)['id']")
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
  • This works, but only when I write it into one line. Extra credit: I like the visibility of this code much more than my one-liner. Do you know what the issue could be? The backslashes make the command throw errors. – jagdpanzer Jan 24 '17 at 16:45
  • Dunno what the issue might by—I just slapped to backslashes and line breaks in there quickly for readability. Horizontal scrolling sucks… – sideshowbarker Jan 24 '17 at 16:47
  • For sure does. The purpose of the backslash is for the shell to read it as one line, right? It seems to throw errors related to the Python import. I'll likely start a question about this. Thank you for your help! – jagdpanzer Jan 24 '17 at 16:49
  • 1
    Yeah, as far as newlines, the backslash normally tells the shell to ignore the newline and treat the current line and following line as one. I might have left a space after one of the backslashes in the snippet. If it did that is one things that would cause it to fail to treat the two lines as one. I’ll check – sideshowbarker Jan 24 '17 at 16:54
  • Well, no spaces in there after all. probably something else I’m overlooking. But anyway as far as bash code snippets at Stackoverflow go, I guess you can consider the backslashes as minimally just a convention for saying, “Wrap the next line into this one—manually if you have to—before trying this.” For better or worse, I guess a lot of people sometimes type in answers here fast without always double-checking (I know I do at least). The point is just to get a workable answer in there. Sometimes somebody else comes along and makes edits to clean up the glitches – sideshowbarker Jan 24 '17 at 16:58