更新 JSON 内容时路径是可变的。

huangapple go评论66阅读模式
英文:

jq : update json content when path is variable

问题

I have the following dataset in a file:

{
   "storageSummary": 6.8624392E13,
   "audit1": {
         "auditScore": 1,
         "suspensionScore": 1,
         "onlineScore": 0.99743587,
         "satelliteName": "us2.storj.io:7777"
      },
   "audit2": {
         "auditScore": 1,
         "suspensionScore": 1,
         "onlineScore": 0.9992917,
         "satelliteName": "saltlake.tardigrade.io:7777"
      },
   "audit3": {
         "auditScore": 1,
         "suspensionScore": 1,
         "onlineScore": 0.99930555,
         "satelliteName": "ap1.storj.io:7777"
      }
}

I want to update the content (I'm storing the JSON into a variable, since I make several updates, then I'll create a new file), but the path of the data to update is variable:

For example, I have:

DS_SRC_FILE="test.json";

DS_OBJECT=$(jq -c '' ${DS_SRC_FILE})

ds="audit1"
new_name="toto"

jq --arg ds ${ds} --arg name ${new_name} '.[]|select(.value==$ds)|=(.satellitename = $name)' <<< ${DS_OBJECT}

So, of course, this is failing with parse error: Invalid numeric literal at line 2, column 36.

What's the right way to do that, please?

英文:

I have the following dataset in a file :

{
   &quot;storageSummary&quot;: 6.8624392E13,
   &quot;audit1&quot;: {
         &quot;auditScore&quot;: 1,
         &quot;suspensionScore&quot;: 1,
         &quot;onlineScore&quot;: 0.99743587,
         &quot;satelliteName&quot;: &quot;us2.storj.io:7777&quot;
      },
   &quot;audit2&quot;: {
         &quot;auditScore&quot;: 1,
         &quot;suspensionScore&quot;: 1,
         &quot;onlineScore&quot;: 0.9992917,
         &quot;satelliteName&quot;: &quot;saltlake.tardigrade.io:7777&quot;
      },
   &quot;audit3&quot;: {
         &quot;auditScore&quot;: 1,
         &quot;suspensionScore&quot;: 1,
         &quot;onlineScore&quot;: 0.99930555,
         &quot;satelliteName&quot;: &quot;ap1.storj.io:7777&quot;
      }
}

I want to update the content (I'm storing the json into a variable, since I make several updates, then I'll create a new file), but the path of the data to update is variable :

For example, I have :

DS_SRC_FILE=&quot;test.json&quot;

DS_OBJECT=$(jq -c &#39;&#39; ${DS_SRC_FILE})

ds=&quot;audit1&quot;
new_name=&quot;toto&quot;

jq --arg ds ${ds} --arg name ${new_name} &#39;.[]|select(.value==$ds)|=(.satellitename = $name)&#39; &lt;&lt;&lt; ${DS_OBJECT}

So, of course, this is failing with parse error: Invalid numeric literal at line 2, column 36

What's the right way to do that please ?

答案1

得分: 2

JSON不使用单引号,应该使用双引号,或者保持数字不带引号。

该命令仍然会失败:以.[]开头会丢弃对象的键并返回值,所以你无法再访问"audit1"了。

另外,要改变数值,你可以使用普通的=(而不需要select),因为右边的表达式不受上下文影响:

jq --arg ds "$ds" --arg name "$new_name" '.[$ds]["satelliteName"] = $name' <<< "$DS_OBJECT"

还请注意,我给shell变量加了双引号,这样即使它们包含空格,命令也能正常工作。

英文:

JSON doesn't use single quotes. Use double quotes instead, or leave the number unquoted.

The command will still fail: starting with .[] will throw away the keys of the object and return the values, so you don't have access to the "audit1" anymore.

Also, to change the value, you can use the plain = (and without select) as the right hand side is not sensitive to context:

jq --arg ds &quot;$ds&quot; --arg name &quot;$new_name&quot; &#39;.[$ds][&quot;satelliteName&quot;] = $name&#39; &lt;&lt;&lt; &quot;$DS_OBJECT&quot;

Also note that I double quoted the shell variables which should make the command work even if they contained whitespace.

huangapple
  • 本文由 发表于 2023年5月17日 22:49:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定