JQ查询-重构嵌套JSON

qvtsj1bj  于 2023-10-21  发布在  其他
关注(0)|答案(2)|浏览(142)

我有一个JSON,看起来像这样:

{
  "orders": [
    {
      "order": [
        {
          "items": [
            {
              "name": "Item 1",
              "id": [],
              "type": [
                {
                  "name": "Color",
                  "value": [
                    {
                      "value": "blue"
                    }
                  ]
                },
                {
                  "name": "model",
                  "value": [
                    {
                      "value": "Stereo"
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          "items": [
            {
              "name": "Item 2",
              "id": [],
              "type": [
                {
                  "name": "Color",
                  "value": [
                    {
                      "value": "Yellow"
                    }
                  ]
                },
                {
                  "name": "model",
                  "value": [
                    {
                      "value": "NewModel"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "id": "715874"
    },
    {
      "order": [
        {
          "items": [
            {
              "name": "Item 6",
              "id": [],
              "type": [
                {
                  "name": "Range",
                  "value": [
                    {
                      "value": "10"
                    }
                  ]
                },
                {
                  "name": "Type",
                  "value": [
                    {
                      "value": "AllRegion"
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          "items": [
            {
              "name": "Item 4",
              "id": [],
              "type": [
                {
                  "name": "Color",
                  "value": [
                    {
                      "value": "Yellow"
                    }
                  ]
                },
                {
                  "name": "model",
                  "value": [
                    {
                      "value": "OldModel"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "id": "715875"
    }
  ]
}

我正在尝试转换为这种格式

{
  "order": [
    {
      "items": [
        {
          "name": "Item 1",
          "type": {
            "Color": "blue",
            "model": "Stereo"
          }
        },
        {
          "name": "Item 2",
          "type": {
            "Color": "Yellow",
            "model": "NewModel"
          }
        }
      ],
      "id": "715874"
    },
    {
      "items": [
        {
          "name": "Item 6",
          "type": {
            "Color": "blue",
            "Type": "AllRegion"
          }
        },
        {
          "name": "Item 4",
          "type": {
            "Color": "Yellow",
            "model": "OldModel"
          }
        }
      ],
      "id": "715875"
    }
  ]
}

我尝试使用这样的查询,无法实现我正在寻找的格式。
.orders[]|{ orders:[ .order[]|{ .order[].items:.],id:.id } ]}
{订单:[ .订单[]|{订单:[.items[].name[] ],id:联系我们
请帮帮我谢谢提前。我在Python中解析它,如果任何其他Lib可以像JQ一样解析它,那也很有帮助。

shyt4zoc

shyt4zoc1#

使用jq,只需为每个字段定义它应该覆盖和收集的内容:

{
  order: .orders | map({
    items: .order | map(
      .items[] | {name, type} | .type |= (map(
        .value |= .[].value
      ) | from_entries)
    ),
    id
  })
}
{
  "order": [
    {
      "items": [
        {
          "name": "Item 1",
          "type": {
            "Color": "blue",
            "model": "Stereo"
          }
        },
        {
          "name": "Item 2",
          "type": {
            "Color": "Yellow",
            "model": "NewModel"
          }
        }
      ],
      "id": "715874"
    },
    {
      "items": [
        {
          "name": "Item 6",
          "type": {
            "Range": "10",
            "Type": "AllRegion"
          }
        },
        {
          "name": "Item 4",
          "type": {
            "Color": "Yellow",
            "model": "OldModel"
          }
        }
      ],
      "id": "715875"
    }
  ]
}

Demo

lmvvr0a8

lmvvr0a82#

我会使用JSON模块。假设你的原始json存储为original_data
然后,您可以将其转换为您想要的格式:

  • 对于每个订单,它创建一个新条目
  • 对于订单中的每个项目,提取项目的名称并将“类型”信息组织到字典中,其中“名称”成为键,“值”成为相应的值。
  • 转换后的数据存储在新的数据结构中
  • 使用.dumps将新结构存储为json

所以代码应该是这样的:

import json
    # Initialize the result structure
    result_data = {"order": []}
    
    # Iterate through "orders"
    for order_info in original_data["orders"]:
        order = {"items": [], "id": order_info["id"]}
    
        # Iterate through the "order" items
        for item in order_info["order"]:
            item_data = {
                "name": item["items"][0]["name"],
                "type": {}
            }
    
            # Iterate through "type" 
            for type_entry in item["items"][0]["type"]:
                item_data["type"][type_entry["name"]] = type_entry["value"][0]["value"]
    
            order["items"].append(item_data)
    
        result_data["order"].append(order)
    
    # Convert the result to JSON format
    result_json = json.dumps(result_data, indent=2)
    
    # Print the resulting JSON
    print(result_json)

这将产生以下JSON(如所需):

{
  "order": [
    {
      "items": [
        {
          "name": "Item 1",
          "type": {
            "Color": "blue",
            "model": "Stereo"
          }
        },
        {
          "name": "Item 2",
          "type": {
            "Color": "Yellow",
            "model": "NewModel"
          }
        }
      ],
      "id": "715874"
    },
    {
      "items": [
        {
          "name": "Item 6",
          "type": {
            "Range": "10",
            "Type": "AllRegion"
          }
        },
        {
          "name": "Item 4",
          "type": {
            "Color": "Yellow",
            "model": "OldModel"
          }
        }
      ],
      "id": "715875"
    }
  ]
}

相关问题