Для более сложного разбора JSON я предлагаю использовать python jsonpath module (by Stefan Goessner) -
sudo easy_install -U jsonpath
Пример file.json (из http://goessner.net/articles/JsonPath ) -
{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
Разберите его (извлеките все названия книг с ценой & lt; 10) -
$ cat file.json | python -c "import sys, json, jsonpath; print '\n'.join(jsonpath.jsonpath(json.load(sys.stdin), 'store.book[?(@.price < 10)].title'))"
Выведет -
Sayings of the Century
Moby Dick
ПРИМЕЧАНИЕ. командная строка не включает проверку ошибок. для полного решения с проверкой ошибок вы должны создать небольшой скрипт python и обернуть код с помощью try-except.