我正在嘗試創建一個新檔案,將 country1.json 中定義的物件添加到 world.json 中。本質上:
世界.json
{
"class": "world",
"version": "1.1.0"
}
country1.json
{
"class": "country",
"country_1": {
"class": "city",
"name": "seattle"
}
}
=
world_country1.json
{
"class": "world",
"version": "1.1.0",
"country1": {
"class": "country",
"country_1": {
"class": "city",
"name": "seattle"
}
}
}
使用 country1.json 檔案中物件的檔案名。如果可能的話,我想使用 bash/jq。
謝謝你的幫助,最好的,羅曼
uj5u.com熱心網友回復:
用于input
訪問第二個檔案,并使用重定向>
到另一個檔案
jq '.country1 = input' world.json country1.json > world_country1.json
{
"class": "world",
"version": "1.1.0",
"country1": {
"class": "country",
"country_1": {
"class": "city",
"name": "seattle"
}
}
}
演示
如果您想使用檔案名作為新欄位的名稱,請使用input_filename
并切斷最后 5 個字符(洗掉.json
):
jq '. (input | {(input_filename[:-5]): .})' world.json country1.json > world_country1.json
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/484825.html