Tente isto:
$ cat json.txt | python -c 'import sys; import simplejson as json; \
print "\n".join( [i["_source"]["memberId"] for i in json.loads( sys.stdin.read() )["hits"]["hits"]] )'
Se você já tem
pretty printed
json, por que você não apenas
grep
?
$ cat json.txt | grep memberId
"memberId": "0x7b93910446f91928e23e1043dfdf5bcf",
"memberId": "0x7b93910446f91928e23e1043dfdf5bcG",
Você sempre pode obter um formato bem impresso com o simplejson python para grep
it.
# cat json_raw.txt
{"hits": {"hits": [{"_score": 1, "_type": "Medical", "_id": "02:17447847049147026174478:174159", "_source": {"memberLastName": "Dubofsky", "memberMiddleName": "Prayag", "memberId": "0x7b93910446f91928e23e1043dfdf5bcf", "memberFirstName": "Uri"}, "_index": "2000_270_0"}, {"_score": 1, "_type": "Medical", "_id": "02:17447847049147026174478:174159", "_source": {"memberLastName": "Dubofsky", "memberMiddleName": "Prayag", "memberId": "0x7b93910446f91928e23e1043dfdf5bcG", "memberFirstName": "Uri"}, "_index": "2000_270_0"}], "total": 74, "max_score": 1}, "_shards": {"successful": 8, "failed": 0, "total": 8}, "took": 670, "timed_out": false}
Usar dumps:
# cat json_raw.txt | python -c 'import sys; import simplejson as json; \
print json.dumps( json.loads( sys.stdin.read() ), sort_keys=True, indent=4); '
{
"_shards": {
"failed": 0,
"successful": 8,
"total": 8
},
"hits": {
"hits": [
{
"_id": "02:17447847049147026174478:174159",
"_index": "2000_270_0",
"_score": 1,
"_source": {
"memberFirstName": "Uri",
"memberId": "0x7b93910446f91928e23e1043dfdf5bcf",
"memberLastName": "Dubofsky",
"memberMiddleName": "Prayag"
},
"_type": "Medical"
},
{
"_id": "02:17447847049147026174478:174159",
"_index": "2000_270_0",
"_score": 1,
"_source": {
"memberFirstName": "Uri",
"memberId": "0x7b93910446f91928e23e1043dfdf5bcG",
"memberLastName": "Dubofsky",
"memberMiddleName": "Prayag"
},
"_type": "Medical"
}
],
"max_score": 1,
"total": 74
},
"timed_out": false,
"took": 670
}
Depois disso, basta grep
resultar com o padrão 'memberId'.
Para ser completamente preciso:
#!/bin/bash
filename="$1"
cat $filename | python -c 'import sys; import simplejson as json; \
print json.dumps( json.loads( sys.stdin.read() ), sort_keys=True, indent=4)' | \
grep memberId | awk '{print $2}' | sed -e 's/^"//g' | sed -e 's/",$//g'
Uso:
$ bash bash.sh json_raw.txt
0x7b93910446f91928e23e1043dfdf5bcf
0x7b93910446f91928e23e1043dfdf5bcG