在我第一次使用shell脚本和json解析器(即jq库)时。
我想将文件1和文件2的一个属性的详细信息与另一个文件3合并到另一个文件3中。
能够合并文件1和文件2的属性,但无法作为完整详细信息呈现在文件3中。
基本上无法将存储的值分配给file3中的特定键属性
j1.json
{
"catalog": {
"git_branch_name": "Release_Sprint1912",
"git_commit_number": "7ff5358b6d919228ae6043f7bb64abd890c6dfe9",
"git_repo_name": "catalog"
},
"swirl_components": {
"git_branch_name": "Release_Sprint1912",
"git_commit_number": "78ed0b1d0e25a261b89160a037530cf6c594892f",
"git_repo_name": "swirl_components"
},
"demo_app": {
"git_repo_name": "demo_app",
"git_branch_name": "Release_Sprint1912",
"git_commit_number": "1f227bb19343d19629b302c425adb8f21f4fab5a"
}
}
j2.json
{
"swirl_app": {
"version": "readBuildInfo_311",
"image_name": "/swirl_app:readBuildInfo_311",
"date_time": "Mon Mar 25 05:55:45 UTC 2019",
"git_latest_commit": "4b7bef6970f83e6e7fc04302c8b29d7cfd4a1885",
"jenkins_build_url": "http://10.229.6.14/job/HC_Dev/job/Code-Build/job/swirl_app/311/",
"artifactory_path": "ARTIFACTORY PATH"
}
}
预期输出:
{
"catalog": {
"git_branch_name": "Release_Sprint1912",
"git_commit_number": "7ff5358b6d919228ae6043f7bb64abd890c6dfe9",
"git_repo_name": "catalog"
},
"swirl_components": {
"git_branch_name": "Release_Sprint1912",
"git_commit_number": "78ed0b1d0e25a261b89160a037530cf6c594892f",
"git_repo_name": "swirl_components"
},
"demo_app": {
"git_repo_name": "demo_app",
"git_branch_name": "Release_Sprint1912",
"git_commit_number": "1f227bb19343d19629b302c425adb8f21f4fab5a"
},
"swirl_app": {
"git_repo_name": "demo_app",
"git_branch_name": "Release_Sprint1912",
"git_commit_number": "1f227bb19343d19629b302c425adb8f21f4fab5a",
"version": "readBuildInfo_311",
"image_name": "/swirl_app:readBuildInfo_311",
"date_time": "Mon Mar 25 05:55:45 UTC 2019",
"git_latest_commit": "4b7bef6970f83e6e7fc04302c8b29d7cfd4a1885",
"jenkins_build_url": "http://10.229.6.14/job/HC_Dev/job/Code-Build/job/swirl_app/311/",
"artifactory_path": "ARTIFACTORY PATH"
}
}
尝试:
j1=$(echo jq "." j1.json)
j2=$(echo jq "." j2.json)
demo=$(echo jq '.demo_app' j1.json)
jq -s 'add' $j1 $j2
jq ".swirl_app" j2.json >> swirl.json
jq '.demo_app' j1.json >> demo.json
jq -s 'add' demo.json swirl.json
total=$(echo jq -s 'add' demo.json swirl.json)
失败或尝试:
jq ".swirl_app={}" j3.json // makes empty object of "swirl_app": {}
jq ".swirl_app=$total" j3.json // this fails as expect result
jq ".swirl_app|=$total" j3.json // this too fails as expected result
我的想法: 1)我们应该使它数组吗?然后赋值 2)上面的变量分配工作正常,但是做错了吗?
最终: 在这方面需要帮助,以了解我的问题所在。
答案 0 :(得分:1)
将文件插入其中,以便可以将它们合并,然后相应地更新对象。
$ jq -s 'add | .swirl_app = .demo_app + .swirl_app' j{1,2}.json > j3.json
{
"catalog": {
"git_branch_name": "Release_Sprint1912",
"git_commit_number": "7ff5358b6d919228ae6043f7bb64abd890c6dfe9",
"git_repo_name": "catalog"
},
"swirl_components": {
"git_branch_name": "Release_Sprint1912",
"git_commit_number": "78ed0b1d0e25a261b89160a037530cf6c594892f",
"git_repo_name": "swirl_components"
},
"demo_app": {
"git_repo_name": "demo_app",
"git_branch_name": "Release_Sprint1912",
"git_commit_number": "1f227bb19343d19629b302c425adb8f21f4fab5a"
},
"swirl_app": {
"git_repo_name": "demo_app",
"git_branch_name": "Release_Sprint1912",
"git_commit_number": "1f227bb19343d19629b302c425adb8f21f4fab5a",
"version": "readBuildInfo_311",
"image_name": "/swirl_app:readBuildInfo_311",
"date_time": "Mon Mar 25 05:55:45 UTC 2019",
"git_latest_commit": "4b7bef6970f83e6e7fc04302c8b29d7cfd4a1885",
"jenkins_build_url": "http://10.229.6.14/job/HC_Dev/job/Code-Build/job/swirl_app/311/",
"artifactory_path": "ARTIFACTORY PATH"
}
}