user2720970
user2720970

Reputation: 294

Curl JSON to output only one object

I would like to extract a single values from the output of a json file.

curl cli.fyi/8.8.8.8

Outputs

{
"type": "IP Address",
"data": {
    "organisation": "Google LLC",
    "country": "United States",
    "countryCode": "US",
    "continent": "North America",
    "latitude": "37.751",
    "longitude": "-97.822"
}
}

I can run this:

curl cli.fyi/8.8.8.8 2>/dev/null | awk -F'"' '$2=="organisation"'

and it will output:

"organisation": "Google LLC",

How can I get only Google LLC ?

Upvotes: 2

Views: 2207

Answers (1)

larsks
larsks

Reputation: 311615

If you need to parse JSON, your best bet is to use an actual JSON parser rather than trying to do it with awk or similar. The jq tool is great for this:

curl cli.fyi/8.8.8.8 | jq -r .data.organization

That would give you the bare string:

Google LLC

Upvotes: 7

Related Questions