Dmitriy Fialkovskiy
Dmitriy Fialkovskiy

Reputation: 3225

open invalid json file in python

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.

Upvotes: 0

Views: 666

Answers (1)

Huang
Huang

Reputation: 609

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}]

Upvotes: 2

Related Questions