This is a pretty basic question, but I don't know Python at all and I ask you guys.
I want to parse a JSON string on Linux command using Python.
and I want to print if only the value of the id column in the JSON string is "ok".
For example,
extected (1) -> if id == ok$ echo '{"id": "ok", "name": "b"}' | python -c 'import json,sys; `blah blah blah.....'
{"id": "ok", "name": "b"}
extected (2) -> if id != ok
$ echo '{"id": "no", "name": "a"}' | python -c 'import json,sys; `blah blah blah.....'
(empty)
I tried many attempts to solve this problem, but all failed..
echo '{"id":"ok", "name": "a"}' | python -c 'import json,sys; d=sys.stdin; obj=json.load(d); print (d if obj["id"] == "ok" else "")'
<open file '<stdin>', mode 'r' at 0x7fb6391060c0>
So I thought variable d is an object, not a value. So I tried to use read().
echo '{"id":"ok", "name": "a"}' | python -c 'import json,sys; d=sys.stdin; obj=json.load(d); print (d.read() if obj["id"] == "ok" else "")'
(empty)
I don't know understand why nothing is printed..
Please help me T.T