Json使用Linux shell脚本解析

时间:2015-11-05 05:01:40

标签: json shell

这是我要解析的json文件。我特别需要json数组中的json对象。 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 个答案:

答案 0 :(得分:1)

使用除shell之外的任何东西。

自从原始答案我找到了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脚本中解析它。