简体   繁体   English

Json使用Linux Shell脚本进行解析

[英]Json parsing with Linux shell script

Here is the json file I want to parse. 这是我要解析的json文件。 I specially need the json objects inside the json array. 我特别需要json数组中的json对象。 Shell script is the only tool I can use right now. 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"
    }
  ]
}

How can I do it in shell script? 如何在Shell脚本中执行此操作?

Use something, anything other than shell. 使用任何东西,除了外壳。

Since the original answer I've found jq : 从原始答案开始,我发现jq

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

Install node.js 安装node.js

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

Install jsawk 安装jsawk

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

Install Ruby 安装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. 只是不要尝试在shell脚本中解析它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM