如何使用 jq 将字段插入 JSON 文件?

How to insert fields into a JSON file using jq?

假设我有一个 JSON 文件 recipe.json 包含以下内容:

{
            "name": "Curried Lentils and Rice",
            "ingredients": [
                {
                    "quantity": "1 quart",
                    "name": "beef broth",
                    "type": "Misc"
                },
                {
                    "quantity": "1 cup",
                    "name": "dried green lentils",
                    "type": "Misc"
                },
                {
                    "quantity": "1/2 cup",
                    "name": "basmati rice",
                    "type": "Misc"
                },
                {
                    "quantity": "1 tsp",
                    "name": "curry powder",
                    "type": "Condiments"
                },
                {
                    "quantity": "1 tsp",
                    "name": "salt",
                    "type": "Condiments"
                }
            ],
            "steps": [
                "Bring broth to a low boil.",
                "Add curry powder and salt.",
                "Cook lentils for 20 minutes.",
                "Add rice and simmer for 20 minutes.",
                "Enjoy!"
            ],
            "timers": [
                0,
                0,
                20,
                20,
                0
            ],
            "imageURL": "http://dagzhsfg97k4.cloudfront.net/wp-content/uploads/2012/05/lentils3.jpg"
}

我正在尝试 insert 新的 JSON 字段到文件的特定位置,使用 jq 如:

"cuisine": "meditaranean"

将成为第2个条目,并且

"meal": "lunch"

将成为第4个条目。所以命令后的文件是这样的:

{
            "name": "Curried Lentils and Rice",
            "cuisine": "meditaranean"
            "ingredients": [
                {
                    "quantity": "1 quart",
                    "name": "beef broth",
                    "type": "Misc"
                },
                {
                    "quantity": "1 cup",
                    "name": "dried green lentils",
                    "type": "Misc"
                },
                {
                    "quantity": "1/2 cup",
                    "name": "basmati rice",
                    "type": "Misc"
                },
                {
                    "quantity": "1 tsp",
                    "name": "curry powder",
                    "type": "Condiments"
                },
                {
                    "quantity": "1 tsp",
                    "name": "salt",
                    "type": "Condiments"
                }
            ],
            "meal": "lunch"
            "steps": [
                "Bring broth to a low boil.",
                "Add curry powder and salt.",
                "Cook lentils for 20 minutes.",
                "Add rice and simmer for 20 minutes.",
                "Enjoy!"
            ],
            "timers": [
                0,
                0,
                20,
                20,
                0
            ],
            "imageURL": "http://dagzhsfg97k4.cloudfront.net/wp-content/uploads/2012/05/lentils3.jpg"
}

我的问题是如何使用 jq?

注意:这个 解决了执行单个字段的更新,而当前的问题是关于插入的。它们就像橙色和黄色的灯笼椒一样不同,这是不同的! (不要让我在这个 post 上添加甜椒的图片,我发誓如果必须的话我会这样做的。)

借助辅助函数,任务变得微不足道:

def insert_kv($key; $value; $ix):
  to_entries
  | .[0:$ix] + [{key: $key, value: $value}] + .[$ix:]
  | from_entries;

insert_kv("cuisine"; "mediterranean"; 1)
| insert_kv("meal"; "lunch"; 3)

您可以(替代地或另外地)定义:

def insert_kv($object; $ix):
  to_entries
  | .[0:$ix] + ($object|to_entries) + .[$ix:]
  | from_entries;

你的json保存到meals.json,执行下面的命令就会得到你需要的结果。

jq '.cusine="mediteranean"' meals.json