Auritro Auritro -4 years ago 129
JSON Question

Json parsing with Linux shell script

Here is the json file I want to parse. I specially need the json objects inside the json array. Shell script is the only tool I can use right now.

{
"entries": [
{
"author": {
"value": "plugin-demo Administrator",
"origin": "http://localhost:8080/webservice/person/18"
},
"creator": {
"value": "plugin-demo Administrator",
"origin": "http://localhost:8080/webservice/person/18"
},
"creationDate": "2015-11-04T15:14:18.000+0600",
"lastModifiedDate": "2015-11-04T15:14:18.000+0600",
"model": "http://localhost:8080/plugin-editorial/model/281/football",
"payload": [
{
"name": "basic",
"value": "Real Madrid are through"
}
],
"publishDate": "2015-11-04T15:14:18.000+0600"
},
{
"author": {
"value": "plugin-demo Administrator",
"origin": "http://localhost:8080/webservice/person/18"
},
"creator": {
"value": "plugin-demo Administrator",
"origin": "http://localhost:8080/webservice/person/18"
},
"creationDate": "2015-11-04T15:14:18.000+0600",
"lastModifiedDate": "2015-11-04T15:14:18.000+0600",
"model": "http://localhost:8080/plugin-editorial/model/281/football",
"payload": [
{
"name": "basic",
"value": "Real Madrid are through"
}
],
"publishDate": "2015-11-04T15:14:18.000+0600"
}
]
}


How can I do it in shell script?

Answer Source

Use something, anything other than shell.

Since the original answer I've found jq:

jq '.entries[0].author.value' /tmp/data.json 
"plugin-demo Administrator"

Install node.js

node -e 'console.log(require("./data.json").entries[0].author.value)'

Install jsawk

cat data.json | jsawk 'return this.entries[0].author.value'

Install Ruby

ruby -e 'require "json"; puts JSON.parse(File.read("data.json"))["entries"][0]["author"]["value"]'

Just don't try and parse it in shell script.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download