2015-11-05 142 views
0

這是我想解析的json文件。我特別需要json數組內的json對象。 Shell腳本是我現在可以使用的唯一工具。Json使用Linux shell腳本解析

{ 
    "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" 
    } 
    ] 
} 

如何在shell腳本中執行此操作?

回答

1

使用一些東西,而不是外殼。

由於原來的答案,我發現jq

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

安裝node.js

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

安裝jsawk

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

安裝Ruby

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

只是不要試圖在shell腳本中解析它。