使用jq将json对象动态添加到数组中

使用jq将json对象动态添加到数组中,json,bash,command-line,jq,Json,Bash,Command Line,Jq,下面是我的employee.json文件的模板 { "orgConfig": { "departments": [] } } 其中,部门将具有如下所示的部门阵列 { "name" : "physics", "id" : "1234", "head" : "abcd" } 同样地 { "name" : "chemistry", "id" : "3421", "head" : "xyz" } 因此,我要构造的最终数

下面是我的employee.json文件的模板

{
    "orgConfig": {
        "departments": []
    }
}
其中,部门将具有如下所示的部门阵列

{
    "name" : "physics",
    "id" : "1234",
    "head" : "abcd"
}
同样地

{
    "name" : "chemistry",
    "id" : "3421",
    "head" : "xyz"
}
因此,我要构造的最终数组结构如下所示

{
    "orgConfig": {
        "departments": [
            {
                "name" : "physics",
                "id" : "1234",
                "head" : "abcd"
            },
            {
                "name" : "chemistry",
                "id" : "3421",
                "head" : "xyz"
            },
            {
                "name" : "Maths",
                "id" : "4634",
                "head" : "jklm"
            }
        ]
    }
}
下面是我将json元素动态添加到departments数组的代码

#!/bin/bash

source department.properties   # will have departments=physiscs,chemistry,Maths,computers .. etc
IFS=',' read -ra NAMES <<< "$departmentsToImport"

position=0
for i in "${NAMES[@]}"; do
    #./jsonfiles will chemistry.json, physics.json, Maths.json etc
    value=`cat ./jsonfiles/$i.json`    

    if [ $position -eq 0 ]
    then
       cat employee.json | jq --arg value "$value" '.orgConfig.departments[0] |= .+ $value' > tmp.json && mv tmp.json employee.json
    else
       cat employee.json | jq --arg position "$position" value "$value" '.orgConfig.departments[$position] |= .+ $value' > tmp.json && mv tmp.json employee.json
    fi
    ((position++))
    rm -rf tmp.json
done

exit $?
我不知道我有多少部门的关键价值图。我无法硬编码索引。对上述问题有什么帮助并将json对象动态添加到数组中吗


谢谢

不用多次调用jq就可以完成任务

类似于以下内容的内容就足够了:

jq -s '{orgConfig: {departments: . }}' jsonfiles/*.json
当然,此解决方案假定.json文件都包含有效的json


诀窍是使用-s选项,因为这会将输入转换为数组。您可能会发现使用-s比其他一些方法可以获得更好的运行时间。

这可以通过关闭
jq
的自动输入读取来实现,然后通过过滤器将第一个显式输入管道化,该过滤器使用剩余的显式输入修改其输入。有道理?否:)但代码本身很简单:

jq -n 'input | .orgConfig.departments += [inputs]' \
  employee.json chemistry.json physics.json math.json
  • 第一个
    输入
    读取第一个参数(
    employee.json
  • +=
    输入
    过滤器获取其输入。它的左操作数 选择要更新的字段;右操作数提供值 以更新它
  • 输入
    读取剩余的命令行参数,并将其内容放入数组中,每个文件位于单独的元素中
  • 结合shell代码选择正确的课程文件

    source department.properties
    IFS=, read -ra NAMES <<< "$departmentsToImport"
    for c in "${NAMES[@]}"; do
        courses+=("./jsonfiles/$c.json")
    done
    jq -n 'input | .orgConfig.departments += [inputs]' employee.json "${courses[@]}"
    
    sourcedepartment.properties
    
    IFS=,read-ra NAMES感谢@peak,在上面给出的解决方案的基础上得到了新的需求。感谢@chepner的大力帮助。解决了我的问题。
    jq -n 'input | .orgConfig.departments += [inputs]' \
      employee.json chemistry.json physics.json math.json
    
    source department.properties
    IFS=, read -ra NAMES <<< "$departmentsToImport"
    for c in "${NAMES[@]}"; do
        courses+=("./jsonfiles/$c.json")
    done
    jq -n 'input | .orgConfig.departments += [inputs]' employee.json "${courses[@]}"