Parse curl response with sed

7,125

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"
Share:
7,125
Imnl
Author by

Imnl

Updated on September 18, 2022

Comments

  • Imnl
    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
      terdon almost 4 years
      What operating system are you using? Do you have GNU tools?
    • Imnl
      Imnl almost 4 years
      osx @terdon dont have preinstalled jq
    • pLumo
      pLumo almost 4 years
      why don't you just use curl https://api.ipify.org which returns your IP directly... (just asking)
    • Imnl
      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
      Kusalananda almost 4 years
      @Imnl Sorry, but what is "a bash tool"? If you mean you only want to use utilities built into bash, then you will find it extremely difficult to correctly parse JSON.
    • Imnl
      Imnl almost 4 years
      @Kusalananda edited, i tried to say unix default tools.
    • Kusalananda
      Kusalananda almost 4 years
      jq is the easiest, most robust, and stable way to parse JSON on macOS.