0

I want to create an array (looking at object_sale_types in the output) using id_type_sale and sale_type_description, keeping the keys email, date, order_id, store but without repetition (assuming it's the same data).

Input

{
"105": [
    {
        "id_type_sale": 2,
        "email" : null,
        "date" : "2016-05-18",
        "order_id": 105,
        "sale_type_description": "Coffee shop",
        "store": "Ezio store"
    },
    {
        "id_type_sale": 5,
        "order_id": 105,
        "email" : null,
        "date" : "2016-05-18",
        "sale_type_description": "Book shop",
        "store": "Ezio store"
    }
],
"106": [
  {
      "id_type_sale": 3,
      "email" : null,
      "date" : "2016-05-19",
      "order_id": 106,
      "sale_type_description": "Food",
      "store": "Ezio store"
  },
  {
      "id_type_sale": 8,
      "order_id": 106,
      "email" : null,
      "date" : "2016-05-19",
      "sale_type_description": "Articles",
      "store": "Ezio store"
  }]}

Output expect

{
"105":[
    {
        "email":null,
        "date":"2016-05-18",
        "order_id":105,
        "store":"Ezio store",
        "object_sale_types":[
            {
                "id_type_sale":2,
                "sale_type_description":"Coffee shop"
            },
            {
                "id_type_sale":5,
                "sale_type_description":"Book shop"
            }
        ]
    }
],
"106":[
    {
        "email":null,
        "date":"2016-05-19",
        "order_id":106,
        "store":"Ezio store",
        "object_sale_types":[
            {
                "id_type_sale":3,
                "sale_type_description":"Food"
            },
            {
                "id_type_sale":8,
                "sale_type_description":"Articles"
            }
        ]
    }
]}

How can I do? What's better approach? I'd like to use python

6
  • 1
    Can you update your question with some repetitive data in the input so that it's clearer what you mean by the output not having repetition? Commented Aug 28, 2020 at 1:26
  • d[key]['object_sale_types'].append(...) Commented Aug 28, 2020 at 1:30
  • Each key like "105" contains the same values for common fields or could it be possible to have different ones? Commented Aug 28, 2020 at 9:13
  • @Let'stry Yes, the same values for commom fields except id_type_sale and sale_type_description that I want to group in array Commented Aug 28, 2020 at 12:16
  • @Barmar About array I understood but how can I leave unique fields for the email, date, order_id, store? Commented Aug 28, 2020 at 12:24

1 Answer 1

1

First you need to load your JSON string with json.loads, then you can iterate over each (key, value) pair in the dictionary, building a new dictionary as you go with the common values from each value object and an array of the id_sale_type and sale_type_description values. Then you can output a new JSON using json.dumps:

d = json.loads(j)
r = {}
for key, value in d.items():
    r[key] = { k : value[0][k] for k in ['email', 'date', 'order_id', 'store'] }
    r[key]['object_sales_types'] = [ { 'id_type_sale' : s['id_type_sale'], 'sale_type_description' : s['sale_type_description'] } for s in value]

print(json.dumps(r, indent=4))

Output:

{
    "105": {
        "email": null,
        "date": "2016-05-18",
        "order_id": 105,
        "store": "Ezio store",
        "object_sales_types": [
            {
                "id_type_sale": 2,
                "sale_type_description": "Coffee shop"
            },
            {
                "id_type_sale": 5,
                "sale_type_description": "Book shop"
            }
        ]
    },
    "106": {
        "email": null,
        "date": "2016-05-19",
        "order_id": 106,
        "store": "Ezio store",
        "object_sales_types": [
            {
                "id_type_sale": 3,
                "sale_type_description": "Food"
            },
            {
                "id_type_sale": 8,
                "sale_type_description": "Articles"
            }
        ]
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.