-2

I am using python to format a JSON file, and push it database. I want to group the values based on Name to get orders information for each customer. I wasn't able to get the right logic to get the common value(Names) out and group it based on the common value. Can anyone please help me. I am new to python and I have written the following logic. The output to this logic is shown below.

for row1 in cb1.n1ql_query(nql):
   col_counts += 1
   result["Category"] = row1['category']
   result["Items"] = row1['item']
   result1["Name"] = row1['name']
   result1 = result
   print(result1)

Output JSON:

[
  {
    "Items": [
      "Item1",
      "Item2",
      "Item3"
    ],
    "Category": "Food",
    "Name": "Rick"
  },
  {
    "Items": [
      "Item1",
      "Item2"
    ],
    "Category": "Drink",
    "Name": "Michael"
  },
  {
    "Items": [
      "Item1"
    ],
    "Category": "Drink",
    "Name": "Rick"
  },
  {
    "Items": [
      "Item1",
      "Item2"
    ],
    "Category": "Food",
    "Name": "Michael"
  },
  {
    "Items": [
      "Item1",
      "Item2",
      "Item3",
      "Items4"
    ],
    "Category": "Accessories",
    "Name": "Rick"
  }
]

I want the JSON in the following format

{
"Rick":[
    {

        "Category": "Food",
        "Items": [
                    "Item1",
                    "Item2",
                    "Item3"
                ]
    },
    {
        "Category": "Drink",
        "Items": [
                    "Item1"
                ]
    },
    {
        "Category": "Accessories",
        "Items": [
                    "Item1",
                    "Item2",
                    "Item3",
                    "Items4"
                ]
    }
],
"Michael":[
    {
        "Category": "Drink",
        "Items": [
                    "Item1",
                    "Item2"
                ]
    },
    {
        "Category": "Food",
        "Items": [
                    "Item1",
                    "Item2"
                ]
    }

]}
Nik
  • 155
  • 1
  • 10
  • 1
    first show us what you have tried so far – Adnan Mar 04 '20 at 14:39
  • 2
    you can't have multiple equal keys in a dictionary, for example `Items`. – marcos Mar 04 '20 at 14:40
  • You can not user same key again like **Category** you used more then one. – krishna Mar 04 '20 at 14:42
  • 1
    Actually, the JSON standard does allow duplicate keys in a JSON object (see https://stackoverflow.com/a/21833017). You can’t have duplicate keys in a Python dictionary, though, which would make Nik’s desired result a little hard to obtain. – gooberwonder Mar 04 '20 at 14:44
  • @Adnan I have updated the question for your reference with the logic – Nik Mar 04 '20 at 15:52
  • @Marcos I made a mistake in the format of my output, have updated the desired result. – Nik Mar 04 '20 at 15:59

2 Answers2

1

I think that what you want is this:

resul = collections.defaultdict(lambda : collections.defaultdict(list))
for data in js:
    resul[data['Name']][data['Category']].extend(data['Items'])

It would give:

{
  "Rick": {
    "Food": [
      "Item1",
      "Item2",
      "Item3"
    ],
    "Drink": [
      "Item1"
    ],
    "Accessories": [
      "Item1",
      "Item2",
      "Item3",
      "Items4"
    ]
  },
  "Michael": {
    "Drink": [
      "Item1",
      "Item2"
    ],
    "Food": [
      "Item1",
      "Item2"
    ]
  }
}
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

As already pointed out in the comments, you cannot duplicate keys in json, because they are unique. But you can combine the results

original = {}  # your dict
result = []
names = set(x.get('Name') for x in original)

for name in names:
    items_original = list(filter(lambda x: x.get('Name') == name, original))
    items, category = zip(*((x.get('Items'), x.get('Category')) for x in items_original))
    result.append({"Name": name, "Category": category, "Items": sum(items, [])})

and output

[
   {
      "Name":"Rick",
      "Category":[
         "Food",
         "Drink",
         "Accessories"
      ],
      "Items":[
         "Item1",
         "Item2",
         "Item3",
         "Item1",
         "Item1",
         "Item2",
         "Item3",
         "Items4"
      ]
   },
   {
      "Name":"Michael",
      "Category":[
         "Drink",
         "Food"
      ],
      "Items":[
         "Item1",
         "Item2",
         "Item1",
         "Item2"
      ]
   }
]
Nuchimik
  • 199
  • 7
  • thanks for the solution, if I combine the result then I wont differentiate between Items and Category. I wont know which Items belong to which Category – Nik Mar 04 '20 at 15:55