Reputation: 24606
I would like to use python to parse JSON in batch scripts, for example:
HOSTNAME=$(curl -s "$HOST" | python ?)
Where the JSON output from curl looks like:
'{"hostname":"test","domainname":"example.com"}'
How can I do this with a single line python command?
Upvotes: 19
Views: 36864
Reputation: 349
Run this:
$ python -m json.tool
It reads input from stdin and print out in a nicely. So with your questions:
HOSTNAME=$(curl -s "$HOST" | python -m json.tool )
Upvotes: 0
Reputation: 24606
Based on the JSON below being returned from the curl command ...
'{"hostname":"test","domainname":"example.com"}'
You can then use python to extract the hostname using the python json module:
HOSTNAME=$(curl -s "$HOST" |
python -c \
'import json,sys;print(json.load(sys.stdin)["hostname"])')
Note that I have split the line using a \
to make it more readable on stackoverflow. I've also simplified the command based on chepner's comment.
Original source: Parsing JSON with Unix tools
See also: https://wiki.python.org/moin/Powerful%20Python%20One-Liners
Upvotes: 23
Reputation: 3691
Since Python is multiplatform, is important to note differences between Linux and Windows, especially because of how they treat double-quotes/single-quotes differently.
Second, some previous answers are a little bit obsolete: in python2, print
without parentheses was permitted. Nevertheless, in python3, print must be between parentheses.
It doesn't matter how you put double/single quotes. Json can be parsed in both ways with "keys" or 'keys'
HOSTNAME=$(curl -s "$HOST" |
python3 -c 'import json,sys;print(json.load(sys.stdin)["hostname"])')
It also works: (pay attention at single/double quote at key)
HOSTNAME=$(curl -s "$HOST" |
python3 -c "import json,sys;print(json.load(sys.stdin)['hostname'])")
Keys in json MUST be between single quotes. Only the following syntax is accepted.
The ConvertTo-Json
function generates object and works with keys between single quotes.
$HOSTNAME=(Invoke-RestMethod $HOST | `
ConvertTo-Json | `
python3 -c "import json,sys; print(json.load(sys.stdin)['hostname'])")
Upvotes: 6
Reputation: 1055
echo '{"hostname":"test","domainname":"example.com"}' | python -m json.tool
Upvotes: 16