使用jq命令将JSON值传递到新的JSON文件

时间:2018-10-10 09:03:25

标签: bash jq

我运行curl命令,然后解析了值(“ id”)。

请求:

curl "http://192.168.22.22/test/index/limit:1/page:1/sort:id/pag1.json" | jq -r '.[0].id'

卷曲响应:

[
  {
    "id": "381",
    "org_id": "9",
    "date": "2018-10-10",
    "info": "THIS IS TEST",
    "uuid": "5bbd1b41bc",
    "published": 1,
    "an": "2",
    "attribute_count": "4",
    "orgc_id": "8",
    "timestamp": "1",
    "dEST": "0",
    "sharing": "0",
    "proposal": false,
    "locked": false,
    "level_id": "1",
    "publish_timestamp": "0",
    "disable_correlation": false,
    "extends_uuid": "",
    "Org": {
      "id": "5",
      "name": "test",
      "uuid": "5b9bc"
    },
    "Orgc": {
      "id": "1",
      "name": "test",
      "uuid": "5b9f93bdeac1b41bc"
    },
    "ETag": []
  }
]

jq响应:

381

现在,我尝试获取“ id”号381,然后在将“ id”号放在正确的位置时在磁盘上创建一个新的JSON文件。

例如,新的JSON文件:

{
  "request": {
    "Event": {
      "id": "381",
      "task": "new"
    }
  }
}

1 个答案:

答案 0 :(得分:1)

给出您的输入,此方法有效:

jq -r '{"request": {"Event": {"id": .[0].id, "task": "new"}}}' > file