Warm tip: This article is reproduced from serverfault.com, please click

linux-使用文本文件中的数据更新JSON文件中的列表

(linux - Update a list in a JSON file with data from a text file)

发布于 2020-11-23 19:49:50

下午好,我有以下dns.json文件,我在其中传递了URL:

{
        "CallerReference": "1605534454287",
        "Aliases": {
            "Quantity": 2,
            "Items": [
                "tester1.university.com.br",
                "tester2.university.com.br",
                "tester3.university.com.br",
                "end.university.com.br"
            ]
        },

然后,我有一个名为list.txt的文件,其中包含以下几行:

tester1.university.com.br
tester2.university.com.br
tester3.university.com.br
tester4.university.com.br
tester5.university.com.br
tester6.university.com.br
tester7.university.com.br
tester8.university.com.br

我需要读取list.txt文件并将其自动插入dns.json中,它看起来像这样:

{
        "CallerReference": "1605534454287",
        "Aliases": {
            "Quantity": 2,
            "Items": [
                "tester1.university.com.br",
                "tester2.university.com.br",
                "tester3.university.com.br",
                "tester4.university.com.br",
                "tester5.university.com.br",
                "tester6.university.com.br",
                "tester7.university.com.br",
                "tester8.university.com.br",
                "end.university.com.br"
            ]
        }

我在外壳方面有很多困难,你们能帮我吗?

Questioner
guil_ads
Viewed
11
community wiki 2020-11-24 03:57:54

使用jq,这是专门为查询和更新JSON而构建的工具:

jq --rawfile newListStr list.txt '
  [$newListStr | split("\n")[] | select(. != "")] as $newList |
  ["end.university.com.br"] as $suffix |
  .Aliases.Items = $newList + $suffix
' <in.json >out.json