0

I have a URL endpoint that I want to retrieve a value from a JSON array. If I use the following in a linux command line, it works perfectly:

curl -s http://10.10.1.10/api/ping | python -c 'import sys, json; print json.load(sys.stdin)["service"]'

Where "service" is the member I'm trying to access. I get a valid output such as Collection Service I want to assign this output to a variable in a bash script.

#!/bin/bash
SERVICE= "$(curl -s http://10.10.1.10/api/ping)" | python -c 'import sys, json; print json.load(sys.stdin)["service"]'

However, this just gives me an error, saying that my JSON array is not a command.

Is this possible? What am I doing wrong?

2 Answers 2

4

You have some odd quoting and bracing in your example. I think if you just take the command that works to print the value you want and put it in $(...) you'll get what you want, so it would be like

SERVICE=$(curl -s http://10.10.1.10/api/ping | python -c 'import sys, json; print json.load(sys.stdin)["service"]')
Sign up to request clarification or add additional context in comments.

Comments

4

You can use ` backticks to capture a value from a command:

SERVICE=`curl -s http://10.10.1.10/api/ping | python -c 'import sys, json; print json.load(sys.stdin)["service"]'`

or put the $(...) around the whole command; `...` and $(...) are equivalent here:

SERVICE=$(curl -s http://10.10.1.10/api/ping | python -c 'import sys, json; print json.load(sys.stdin)["service"]')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.