如何使用jq更新json文档中的单个值? [英] How do I update a single value in a json document using jq?

查看:99
本文介绍了如何使用jq更新json文档中的单个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我忽略了一些非常明显的内容,那么该应用程序;我刚刚找到了jq,并试图用它来更新一个JSON值而不影响周围的数据.

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.

我想将curl结果传送到jq,更新值,然后将更新的JSON传送到curl -X PUT.

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

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

到目前为止,我已经使用sed将其合并在一起,但是在查看jq|=运算符的一些示例之后,我确定我不需要这些.

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.

这是一个JSON示例-在保留其余JSON的同时,如何使用jq设置"local": false?

Here's a JSON sample--how would I use jq to set "local": false, while preserving the rest of the 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

这篇关于如何使用jq更新json文档中的单个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆