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

时间:2018-06-06 21:43:26

标签: json object insert key-value 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"
}

我尝试使用 jq jq 插入新的JSON字段到文件中的特定位置。

"cuisine": "meditaranean"

将成为第二个条目,

"meal": "lunch"

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

{
            "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

执行此操作

注意:此other question地址执行单个字段的更新,而当前的问题是关于插入。它们与橙色和黄色甜椒一样不同,这是不同的! (不要让我在这篇文章中添加甜椒图片,我发誓,如果必须的话,我会这样做。)

2 个答案:

答案 0 :(得分:0)

在辅助函数的帮助下,任务变得微不足道了:

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;

答案 1 :(得分:0)

将您的json保存到meals.json的位置,执行以下命令将完全满足您的需求。

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