Home > other >  format json is not true
format json is not true

Time:07-25

I have a site that send me a json but this json has wrong format and I don't know what I should to do to convert this to the right format . I want to use this to my python code. this is my json that sent by site.

{'product_id': 11, 'user_id': 44, 'data': {'Wood shear wall design': {'Height of wall': {'value': '4', 'unit': 'ft'}, 'Length of wall': {'value': '4', 'unit': 'ft'}, 'Shear force': {'value': '1000', 'unit': 'lb'}, 'sheathing grade': {'value': '{"id":1136,"item":"Sheathing","section_product_item":"30_77"}', 'unit': 'N/A'}, 'sheathing material': {'value': '{"id":1137,"item":"OBS","section_product_item":"30_77"}', 'unit': 'N/A'}, 'sheathing type': {'value': '{"id":1138,"item":"3\\/8","section_product_item":"30_77"}', 'unit': 'N/A'}, 'stud size': {'value': '{"id":1139,"item":"2_3","section_product_item":"30_77"}', 'unit': 'N/A'}, 'number of nails': {'value': '1', 'unit': 'N/A'}, 'stud type': {'value': '{"id":1143,"item":"6d","section_product_item":"30_77"}', 'unit': 'N/A'}, 'nail spacing': {'value': '{"id":1144,"item":"2","section_product_item":"30_77"}', 'unit': 'ft'}, 'modulus of elasticity': {'value': '1', 'unit': 'psf'}, 'HD capacity': {'value': '1', 'unit': 'lb'}, 'HD deflection': {'value': '1', 'unit': 'in'}, 'number of opening': {'value': '{"id":1149,"item":"0","section_product_item":"30_77"}', 'unit': 'N/A'}}}, 'type': 1, 'number_of_projects': 1}

I write this in every online formatter and insomnia but in all of them raise syntax error for json. there is any library or function that can solve this problem?

CodePudding user response:

The text you provided uses single-quotes ('), while json requires double-quotes ("). So to make it valid json you can probably take it as a string and just replace all ' with ".

s = s.replace("'", '"')

Then do json.loads(s) to get a dictionary from the json. Just make sure that there aren't any single-quotes in the json that are actually meant to be single-quotes and don't just annotate keys.

Edit: on a second look the whole "json" looks pretty weird, are you sure this is supposed to be valid json?

CodePudding user response:

Your data is not JSON. It is however a valid string representation of a Python dictionary.

Therefore:

import ast
s = """{'product_id': 11, 'user_id': 44, 'data': {'Wood shear wall design': {'Height of wall': {'value': '4', 'unit': 'ft'}, 'Length of wall': {'value': '4', 'unit': 'ft'}, 'Shear force': {'value': '1000', 'unit': 'lb'}, 'sheathing grade': {'value': '{"id":1136,"item":"Sheathing","section_product_item":"30_77"}', 'unit': 'N/A'}, 'sheathing material': {'value': '{"id":1137,"item":"OBS","section_product_item":"30_77"}', 'unit': 'N/A'}, 'sheathing type': {'value': '{"id":1138,"item":"3\\/8","section_product_item":"30_77"}', 'unit': 'N/A'}, 'stud size': {'value': '{"id":1139,"item":"2_3","section_product_item":"30_77"}', 'unit': 'N/A'}, 'number of nails': {'value': '1', 'unit': 'N/A'}, 'stud type': {'value': '{"id":1143,"item":"6d","section_product_item":"30_77"}', 'unit': 'N/A'}, 'nail spacing': {'value': '{"id":1144,"item":"2","section_product_item":"30_77"}', 'unit': 'ft'}, 'modulus of elasticity': {'value': '1', 'unit': 'psf'}, 'HD capacity': {'value': '1', 'unit': 'lb'}, 'HD deflection': {'value': '1', 'unit': 'in'}, 'number of opening': {'value': '{"id":1149,"item":"0","section_product_item":"30_77"}', 'unit': 'N/A'}}}, 'type': 1, 'number_of_projects': 1}"""

d = ast.literal_eval(s)
  • Related