skip to Main Content

I have a JSON which looks like this

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

I am trying to convert to this format

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

I tried using queries like this, not able to achieve the format i am looking for..

.orders[] | { orders : [ .order[] |{ .order[].items : .], id : .id } ]}

{ orders : [ .orders[] |{ order : [.items[].name[] ], id : .id } ]}

Please help. thanks in Advance. I am parsing this in Python, if any other Lib can parse this similar to JQ thats also helpfull.

2

Answers


  1. Using jq, just define for each field what it should iterate over and collect:

    {
      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

    Login or Signup to reply.
  2. I would do it with the json module. Suppose your original json is stored as original_data.

    Then you can iterate over it to construct your desired format:

    • For each order, it create a new entry
    • For each item in the order, extract the name of the item and organize the "type" information into a dictionary where the "name" becomes a key, and the "value" becomes the corresponding value.
    • This transformed data is stored in a new data structure
    • store the new structure as json with .dumps

    So code would be like this:

    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)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search