0

I'm working on a json file in python like this:

[{
  "_id": {
    "$oid": "60de048cd7905488bbe0e511"
  },
  "item_name": "STOCKHOLM",
  "item_type": "Sofa",
  "item_id": "302.450.44",
  "item_detail": [
    "3paxsofa",
    "seglora"
  ],
  "item_price": "18990",
  "item_count": "35",
  "item_size": {
    "width:": "211 cm",
    "depth:": "88 cm",
    "height:": "80 cm",
    "heigth2:": "14 cm",
    "height3:": "72 cm",
    "depth2:": "158 cm",
    "depth3:": "59 cm",
    "height4:": "43 cm",
    "no:": "3 "
  },
  "item_collapsepk": {
    "product_id: ": "302.450.44",
    "pack: ": "1",
    "depth: ": "216 cm",
    "width: ": "92 cm",
    "height: ": "60 cm",
    "total_weight: ": "71.26 kg",
    "net_weight: ": "56.64 kg",
    "pack_per: ": "1176.0 L"
  },
  "maintenance": [
    "avoid sunset ",
    "clear with wet towel"
  ],
  "item_good": [
    "natural leather ",
    "softer leathwe ",
    "depper color and longer lifespan",
    "Niels Gammelgaard"
  ],
  "item_env": {}
},{
  "_id": {
    "$oid": "60de048fd7905488bbe0e513"
  },
  "item_name": "HEMLINGBY",
  "item_type": "sofa",
  "item_id": "704.343.68",
  "item_detail": [
    "2pax sofa",
    "knisa"
  ],
  "item_price": "899",
  "item_count": "2627",
  "item_size": {
    "width:": "145 cm",
    "depth:": "71.5 cm",
    "height:": "71.5 cm",
    "heigth2:": "14.5 cm",
    "height3:": "61 cm",
    "depth2:": "137 cm",
    "depth3:": "54 cm",
    "height4:": "42 cm"
  },
  "item_collapsepk": {
    "product_id: ": "704.343.68",
    "pack: ": "1",
    "depth: ": "137 cm",
    "width: ": "73 cm",
    "height: ": "32 cm",
    "total_weight: ": "34.32 kg",
    "net_weight: ": "29.86 kg",
    "pack_per: ": "320.0 L"
  },
  "maintenance": [
    "no bleach",
    "no dry"
  ],
  "item_good": [
    "fixed"
  ],
  "item_env": {}
},{
  "_id": {
    "$oid": "60de15b556f3d120e64db051"
  },
  "item_name": "ANGERSBY",
  "item_type": "梳化",
  "item_id": "804.571.18",
  "item_detail": [
    "2paxsofa",
    "knisa "
  ],
  "item_price": "1690",
  "item_count": "842",
  "item_size": {
    "width:": "137 cm",
    "depth:": "84 cm",
    "width1:": "120 cm",
    "depth2:": "49 cm",
    "heigth2:": "46 cm"
  },
  "item_collapsepk": {
    "product_id: ": "804.571.18",
    "pack: ": "1",
    "depth: ": "87 cm",
    "width: ": "62 cm",
    "height: ": "47 cm",
    "total_weight: ": "28.00 kg",
    "net_weight: ": "26.28 kg",
    "pack_per: ": "253.5 L"
  },
  "maintenance": [
    "no bleach",
    "no dry",
    "咕套",
    "fixed",
    "40 degree with noral mode"
  ],
  "item_good": null,
  "item_env": [
    "loner lifespan",
    "60%polyester",
    "40%rubber"
  ]
 }
]

And I want change all the list in the json file to a dictionary for further usage like this, especially for(item_detail,maintance,item_good,item_env):

[{
  "_id": {
    "$oid": "60de048cd7905488bbe0e511"
  },
  "item_name": "STOCKHOLM",
  "item_type": "Sofa",
  "item_id": "302.450.44",
  "item_detail": {
    "3paxsofa",
    "seglora"
  },
  "item_price": "18990",
  "item_count": "35",
  "item_size": {
    "width:": "211 cm",
    "depth:": "88 cm",
    "height:": "80 cm",
    "heigth2:": "14 cm",
    "height3:": "72 cm",
    "depth2:": "158 cm",
    "depth3:": "59 cm",
    "height4:": "43 cm",
    "no:": "3 "
  },
  "item_collapsepk": {
    "product_id: ": "302.450.44",
    "pack: ": "1",
    "depth: ": "216 cm",
    "width: ": "92 cm",
    "height: ": "60 cm",
    "total_weight: ": "71.26 kg",
    "net_weight: ": "56.64 kg",
    "pack_per: ": "1176.0 L"
  },
  "maintenance": {
    "avoid sunset ",
    "clear with wet towel"
  },
  "item_good": {
    "natural leather ",
    "softer leathwe ",
    "depper color and longer lifespan",
    "Niels Gammelgaard"
  },
  "item_env": {}
},{
  "_id": {
    "$oid": "60de048fd7905488bbe0e513"
  },
  "item_name": "HEMLINGBY",
  "item_type": "sofa",
  "item_id": "704.343.68",
  "item_detail": {
    "2pax sofa",
    "knisa"
  },
  "item_price": "899",
  "item_count": "2627",
  "item_size": {
    "width:": "145 cm",
    "depth:": "71.5 cm",
    "height:": "71.5 cm",
    "heigth2:": "14.5 cm",
    "height3:": "61 cm",
    "depth2:": "137 cm",
    "depth3:": "54 cm",
    "height4:": "42 cm"
  },
  "item_collapsepk": {
    "product_id: ": "704.343.68",
    "pack: ": "1",
    "depth: ": "137 cm",
    "width: ": "73 cm",
    "height: ": "32 cm",
    "total_weight: ": "34.32 kg",
    "net_weight: ": "29.86 kg",
    "pack_per: ": "320.0 L"
  },
  "maintenance": {
    "no bleach",
    "no dry"
  },
  "item_good": {
    "fixed"
  },
  "item_env": {}
},{
  "_id": {
    "$oid": "60de15b556f3d120e64db051"
  },
  "item_name": "ANGERSBY",
  "item_type": "梳化",
  "item_id": "804.571.18",
  "item_detail": {
    "2paxsofa",
    "knisa "
  },
  "item_price": "1690",
  "item_count": "842",
  "item_size": {
    "width:": "137 cm",
    "depth:": "84 cm",
    "width1:": "120 cm",
    "depth2:": "49 cm",
    "heigth2:": "46 cm"
  },
  "item_collapsepk": {
    "product_id: ": "804.571.18",
    "pack: ": "1",
    "depth: ": "87 cm",
    "width: ": "62 cm",
    "height: ": "47 cm",
    "total_weight: ": "28.00 kg",
    "net_weight: ": "26.28 kg",
    "pack_per: ": "253.5 L"
  },
  "maintenance": {
    "no bleach",
    "no dry",
    "咕套",
    "fixed",
    "40 degree with noral mode"
  },
  "item_good": null,
  "item_env": {
    "loner lifespan",
    "60%polyester",
    "40%rubber"
  }
 }
]

and the naming can be item_detail1, item_detail2, maintance1, maintance2 etc. I try change the data structure in the python but I fail to get my expected result. How can get this result?

2
  • 3
    Those are not dictionaries: "maintenance": {"no bleach","no dry"}. Dictionaries have keys and values. Commented Jul 12, 2021 at 2:56
  • Oh, Then can I know what is it and how can I change to "maintenance": {"maintance1":"no bleach","maintance2":"no dry"} Commented Jul 12, 2021 at 3:01

1 Answer 1

1

You need to determine if it's a list or a dict, then create a new dict using the list

import json


def json_to_dict(obj, key):
    _dict = {}        
    if type(obj) is dict:
        for key in obj.keys():
            _dict[key] = json_to_dict(obj[key], key)
        return _dict
    if type(obj) is list:
        for idx, child_obj in enumerate(obj):
            _dict[f"{key}{idx}"] = json_to_dict(child_obj, key)
        return _dict
    return obj

with open('product.json', 'r') as f:
    read_data = f.read()
    products = []
    for idx, product in enumerate(json.loads(read_data)):
        products.append(json_to_dict(product, idx))


    print(json.dumps(products, indent=4, sort_keys=True))
        
f.closed

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You for your help! but how can I save it to the json file with correct format?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.