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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31034746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:57:05  来源:igfitidea点击:

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

jsonjq

提问by STW

Appologies if I've overlooked something very obvious; I've just found jqand am trying to use it to update one JSON value without affecting the surrounding data.

如果我忽略了一些非常明显的东西,请道歉;我刚刚发现jq并试图用它来更新一个 JSON 值而不影响周围的数据。

I'd like to pipe a curlresult into jq, update a value, and pipe the updated JSON to a curl -X PUT. Something like

我想将curl结果通过管道传输到jq,更新值,并将更新后的 JSON 通过管道传输到curl -X PUT. 就像是

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 jqI'm sure that I don't need these.

到目前为止,我已经使用 将它破解了sed,但是在查看了几个|=操作符示例之后,jq我确信我不需要这些。

Here's a JSON sample--how would I use jqto set "local": false, while preserving the rest of the JSON?

这是一个 JSON 示例——我将如何使用jqset "local": false,同时保留 JSON 的其余部分?

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

回答by Jeff Mercado

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

回答by Paul Chris Jones

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

回答by Thiago Conrado

a similar function to the operator |= is map. map will be suitable to avoid the requirement of a previous filter for the array...

与操作符 |= 类似的功能是 map。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 的用法中也犯了一些错误,只是考虑它确实“读取”了第一个参数作为程序,因此所有需要的命令都应包含在调用程序。