0

I have a file with such non-valid json data (it's cut for clarity):

[
{
  "orderID": 90,
  "orderDate": '2017-05-10',  #issue №1
  "clientName": "Mr. Bean",
  "clientPhoneN": "123-4567",
  "orderContents": [
    {
      "productID": 05,        #issue №2
      "productName": "Bicycle",
      "quantity": 1,
      "price": 8000
    },
    {
      "productID": 23,
      "productName": "helmet",
      "quantity": 2,
      "price": 1000
    }
  ],
  "orderCompleted": true
}
]

I tried to open it in python and transform it to list of dictionaries, but with no success. Depending on the case I get different errors. It will take too much space to outline all my attempts and their ending errors.

I have two issues here with the file:

issue №1 - single quotes in orderDate value. it results with :

JSONDecodeError: Expecting value

issue №2 - zero leading productID. It results with:

JSONDecodeError: Expecting ',' delimiter

I can hardcode these issues, but I feel that it's not true pythonic way.

Is there an option of "pretty" opening and converting this data file to list of dictionaries?

Most probably I want to keep productID data typa as integer, but if it's impossible, str is ok too.

Dmitriy Fialkovskiy
  • 3,065
  • 8
  • 32
  • 47

1 Answers1

2

Try demjson package:

from demjson import decode
decode("""[
{
  "orderID": 90,
  "orderDate": '2017-05-10',
  "clientName": "Mr. Bean",
  "clientPhoneN": "123-4567",
  "orderContents": [
    {
      "productID": 05,
      "productName": "Bicycle",
      "quantity": 1,
      "price": 8000
    },
    {
      "productID": 23,
      "productName": "helmet",
      "quantity": 2,
      "price": 1000
    }
  ],
  "orderCompleted": true
}
]""")

You'll get:

[{'clientName': 'Mr. Bean',
  'clientPhoneN': '123-4567',
  'orderCompleted': True,
  'orderContents': [{'price': 8000,
    'productID': 5,
    'productName': 'Bicycle',
    'quantity': 1},
   {'price': 1000, 'productID': 23, 'productName': 'helmet', 'quantity': 2}],
  'orderDate': '2017-05-10',
  'orderID': 90}]
Huang
  • 599
  • 2
  • 6
  • I knew that there's something easy to apply=) thanks, Huang! – Dmitriy Fialkovskiy Aug 10 '17 at 19:18
  • For those curious why this module works as opposed to the standard library `json`: "In non-strict mode it can also deal with slightly non-conforming input that is more JavaScript than JSON (such as allowing comments)." – Alex Aug 10 '17 at 19:20