在特定位置添加JSON对象 [英] Add JSON Object at specific location

查看:79
本文介绍了在特定位置添加JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的两个json文件内容:

Here are my two json files content:

file1.json

file1.json

{
  "nodes": [
    {
      "pm_addr": "192.100.0.4",
      "name": "TB1-OSC-A"
    },
    {
      "pm_addr": "192.100.0.6",
      "name": "TB1-OSC-B"
    },
    {
      "pm_addr": "192.100.0.7",
      "name": "TB1-OSC-C"
    }
  ]
}

file2.json

file2.json

{
  "nodes": [
    {
      "pm_addr": "192.100.0.4",
      "name": "TB1-OSC-D"
    }
  ]
}

我想将file2.json的内容添加到file1.json中,但是在json对象之后: TB1-OSC-B ,即第二个json条目.在这种情况下,该条目位于第二位置,但也可以位于其他任何位置.我的最终json文件应如下所示:

I want to add the contents of file2.json to file1.json but after the json object : TB1-OSC-B i.e., second json entry. In this case the entry is present at second location but it may be present any where else aswell. My final json file should look like this:

file3.json

file3.json

{
  "nodes": [
    {
      "pm_addr": "192.100.0.4",
      "name": "TB1-OSC-A"
    },
    {
      "pm_addr": "192.100.0.6",
      "name": "TB1-OSC-B"
    },
    {
      "pm_addr": "192.100.0.4",
      "name": "TB1-OSC-D"
    },
    {
      "pm_addr": "192.100.0.7",
      "name": "TB1-OSC-C"
    }
  ]
}

我使用的是jq版本:1.3,我无法对其进行升级.我尝试使用jq提供的add和= +参数.但这没有帮助.

I am using jq version : 1.3 and I have no way to upgade it. I tried using add and =+ parameters provided by jq. But it did not help.

感谢前进

推荐答案

如果要将对象从 file2.json 附加到 nodes 数组,则非常简单:

If you wanted to append the object from file2.json to the nodes array it is fairly straightforward:

jq -s '.[0].nodes += [ .[1].nodes[0] ] | .[0]' file1.json file2.json

输出:

{
  "nodes": [
    {
      "pm_addr": "192.100.0.4",
      "name": "TB1-OSC-A"
    },
    {
      "pm_addr": "192.100.0.6",
      "name": "TB1-OSC-B"
    },
    {
      "pm_addr": "192.100.0.7",
      "name": "TB1-OSC-C"
    },
    {
      "pm_addr": "192.100.0.4",
      "name": "TB1-OSC-D"
    }
  ]
}

但是,由于要插入对象,因此变得更加棘手,因为 jq AFAIK不支持插入数组,仅支持前置和追加.

However, because you want to insert the object, it becomes more tricky, as jq AFAIK does not support insertion into arrays, only prepending and appending.

下面是一个非常复杂的示例,说明如何通过将其拼接到其中来完成此操作:

Below is a rather convoluted example of how to do this by splicing it in:

parse.jq

# Find the desired index and store it as $n
(.[0].nodes | map(.name == "TB1-OSC-B") | index(true)) as $n   |

# Splice the object from the second file  into the first
.[0].nodes =   .[0].nodes[0:($n|tonumber+1)]   +
             [ .[1].nodes[0] ]                 +
               .[0].nodes[($n|tonumber+1):(.[0].nodes|length)] |

# Only output the now modified object from the first file 
.[0]

像这样运行它:

jq -sf parse.jq file1.json file2.json

在这种情况下的输出:

{
  "nodes": [
    {
      "pm_addr": "192.100.0.4",
      "name": "TB1-OSC-A"
    },
    {
      "pm_addr": "192.100.0.6",
      "name": "TB1-OSC-B"
    },
    {
      "pm_addr": "192.100.0.4",
      "name": "TB1-OSC-D"
    },
    {
      "pm_addr": "192.100.0.7",
      "name": "TB1-OSC-C"
    }
  ]
}

这篇关于在特定位置添加JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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