简体   繁体   English

如何使用 jq 更新 json 文档中的单个值?

[英]How do I update a single value in a json document using jq?

Appologies if I've overlooked something very obvious;如果我忽略了一些非常明显的东西,请道歉; I've just found jq and am trying to use it to update one JSON value without affecting the surrounding data.我刚刚找到jq并试图用它来更新一个 JSON 值而不影响周围的数据。

I'd like to pipe a curl result into jq , update a value, and pipe the updated JSON to a curl -X PUT .我想将curl结果通过管道传输到jq ,更新一个值,并将更新后的 JSON 通过管道传输到curl -X PUT Something like就像是

curl http://example.com/shipping.json | jq '.' field: value | curl -X PUT http://example.com/shipping.json

So far I've hacked it together using sed , but after looking at a few examples of the |= operator in jq I'm sure that I don't need these.到目前为止,我已经使用sed它破解了,但是在查看了jq |=运算符的几个示例后,我确定我不需要这些。

Here's a JSON sample--how would I use jq to set "local": false , while preserving the rest of the JSON?这是一个 JSON 示例——我将如何使用jq设置"local": false ,同时保留 JSON 的其余部分?

{
  "shipping": {
    "local": true,
    "us": true,
    "us_rate": {
      "amount": "0.00",
      "currency": "USD",
      "symbol": "$"
    }
  }
}

You set values of an object using the = operator.您可以使用=运算符设置对象的值。 |= on the other hand is used to update a value. |=另一方面用于更新值。 It's a subtle but important difference.这是一个微妙但重要的区别。 The context of the filters changes.过滤器的上下文发生变化。

Since you are setting a property to a constant value, use the = operator.由于您将属性设置为常量值,因此请使用=运算符。

.shipping.local = false

Just note that when setting a value to a property, it doesn't necessarily have to exist.请注意,在为属性设置值时,它不一定必须存在。 You can add new values easily this way.您可以通过这种方式轻松添加新值。

.shipping.local = false | .shipping.canada = false | .shipping.mexico = true

Update a value (sets .foo.bar to "new value"):更新一个值(将 .foo.bar 设置为“新值”):

jq '.foo.bar = "new value"' file.json

Update a value using a variable (sets .foo.bar to "hello"):使用变量更新值(将 .foo.bar 设置为“hello”):

variable="hello"; jq --arg variable "$variable" '.foo.bar = $variable' file.json

a similar function to the operator |= is map.与操作符 |= 类似的功能是 map。 map will be suitable to avoid the requirement of a previous filter for the array... map 将适用于避免对数组的先前过滤器的要求......

imagine that your data is an array (very common for this example)想象你的数据是一个数组(这个例子很常见)

[
  {
    "shipping": {
      "local": true,
      "us": true,
      "us_rate": {
        "amount": "1.00",
        "currency": "USD",
        "symbol": "$"
      }
    }
  },
  {
    "shipping": {
      "local": true,
      "us": true,
      "us_rate": {
        "amount": "1.00",
        "currency": "USD",
        "symbol": "$"
      }
    }
  }
]

hence it is necessary to consider the array in the code as:因此有必要将代码中的数组视为:

http://example.com/shipping.json | jq '.[] | .shipping.local = "new place"' | curl -X PUT http://example.com/shipping.json

or to use the map function that is crafted to work in every array element as或者使用精心设计的用于在每个数组元素中工作的 map 函数作为

http://example.com/shipping.json | jq 'map(.shipping.local = "new place")' | curl -X PUT http://example.com/shipping.json

Observation观察

For the sake of those that are learning, you also did some mistakes in the jq usage, just consider that it does "read" the 1st parameter as the program, hence all the desired commands shall be included in the very first string after calling the program.为了那些正在学习的人,你在 jq 的用法中也犯了一些错误,只是考虑它确实“读取”了第一个参数作为程序,因此所有需要的命令都应包含在调用程序。

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

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