0

I need to accurately grab a single value out of a JSON data structure Curl'd from an api using the tools that are natively available on an Amazon Linux EC2 instance (Python and CURL) are there by default so I don't have fly anything in via yum/github/curl. The value in question is going to be used for an Amazon User data script to uniquely identify servers in a cluster so compactness is important.

eg:

curl https://superdooperapi.io/get-me-an-id 

Will return

{ "id": 12398990, "date": "2015-01-10" }

I just need the single numeric value for id so I can use it in a shell script. I could use grep/sed/awk but that just seems messy. It's easy to do writing a full python script to execute, but I'd like an elegant single line solution that I can use in a startup script.

Jujhar Singh
  • 3,641
  • 5
  • 29
  • 38
  • possible duplicate of [How can I pretty-print JSON?](http://stackoverflow.com/questions/352098/how-can-i-pretty-print-json) – Aprillion Jan 16 '15 at 12:28

1 Answers1

1

Here's some simple compact python that will do it for you:

curl -s https://superdooperapi.io/get-me-an-id | python -c 'import json,sys;data=json.load(sys.stdin);print data["id"]'

that will return 12398990

Jujhar Singh
  • 3,641
  • 5
  • 29
  • 38