Parse curl response with sed
Solution 1
I would use command substitution instead of piping. On a Linux machine, I would use:
curl "http://my.api.com?query=$(curl https://api.ipify.org?format=json | grep -oP 'ip":"\K[0-9.]+')"
And on a machine that doesn't have GNU tools (e.g. macOS), one of:
curl "http://my.api.com?query=$(curl https://api.ipify.org?format=json | sed -E 's/.*ip":"([0-9.]+).*/\1/')"
Or even
curl "http://my.api.com?query=$(curl https://api.ipify.org?format=json 2>/dev/null | tr -d '"' | sed 's/.*ip:\([0-9.]*\).*/\1/')"
Solution 2
curl 'https://api.ipify.org?format=json' | jq -r '.ip'
This would use jq
to extract the value associated with the top-level ip
key in the JSON response from curl
.
You could then use this to make your other curl
call:
ipaddr=$( curl 'https://api.ipify.org?format=json' | jq -r '.ip' )
curl "http://my.api.com?query=$ipaddr"
Note also that URLs should always be quoted on the command line as they may contain ?
and &
and other characters that the shell would treat specially.
jq
is available through Homebrew on macOS.
Or, you could, as pLumo suggests in comments, just don't request a JSON formatted response from api.ipfy.org
:
ipaddr=( curl 'https://api.ipify.org' )
curl "http://my.api.com?query=$ipaddr"
Imnl
Updated on September 18, 2022Comments
-
Imnl over 1 year
I'm trying to call a JSON API with the following
curl
command on macOS:curl https://api.ipify.org?format=json
It returns something like this:
{"ip":"xxx.xxx.xxx.xxx"}
I would like to extract the IP address from this response and run another
curl
command with this.curl https://api.ipify.org?format=json | curl http://my.api.com?query=<IP RESULT>
Some of my failed attempts involve piping the result through a
sed
command with a regular expression.-
terdon almost 4 yearsWhat operating system are you using? Do you have GNU tools?
-
Imnl almost 4 yearsosx @terdon dont have preinstalled jq
-
pLumo almost 4 yearswhy don't you just use
curl https://api.ipify.org
which returns your IP directly... (just asking) -
Imnl almost 4 years@pLumo Agree 200%, im going still to leave the question because parsing a json response with unix default tools in an easy way stills has interest for me
-
Kusalananda almost 4 years@Imnl Sorry, but what is "a
bash
tool"? If you mean you only want to use utilities built intobash
, then you will find it extremely difficult to correctly parse JSON. -
Imnl almost 4 years@Kusalananda edited, i tried to say unix default tools.
-
Kusalananda almost 4 years
jq
is the easiest, most robust, and stable way to parse JSON on macOS.
-