0

i have json file as below and tried to convert to python dictionary but getting error

{
  "response": {
    "dev_log": {
      "data": [
        {
          "id": "1",
          "timestamp": "2020-01-16 10:11:12",
          "email": "[email protected]"
        },
        {
          "id": "2",
          "timestamp": "2020-02-27 15:33:34",
          "email": "[email protected]"
        },
        {
          "id": "3",
          "timestamp": "2020-02-27 15:34:07",
          "email": "[email protected]"
        }
      ],
      "total_dev_log": "1423"
    },
    "client_log": {
      "data": [
        {
          "customer_city": "LONDON",
          "customer_login": "AAAAAAAAAAAAAA",
          "customer_state": "MC",
          "details": "aaaaaaaaaaa-bbbbbbbbbbbbbbb-cccccccccccccc ",
          "log_number": "1",
          "dept": "Sales",
          "staff_id": "S123",
          "staff_name": "EricY",
          "timestamp": "2020-02-27 15:57:24"
        },
        {
          "customer_city": "SINGAPORE",
          "customer_login": "BBBBBBBBBBBBB",
          "customer_state": "XX",
          "details": "ddddddddd-eeeeeeeeeeee-ffffffffffff ",
          "log_number": "1",
          "dept": "Eng",
          "staff_id": "S456",
          "staff_name": "YongG",
          "timestamp": "2020-02-27 15:57:24"
        }
      ],
      "total_hero_query": "13"
    },
    "response_time": "0.723494",
    "transaction_id": "909122",
    "transaction_status": "OK",
    "transaction_time": "Fri Feb 28 15:27:51 2020"
  }
}

I'm able to view as a valid json via http://jsonviewer.stack.hu. I believed its a valid json string format.

Normallay I just use code below to read json file and convert it to dict but I'm getting error.

with open('datfile.json', 'r') as f:
   datDict = json.load(f)

Error

Traceback (most recent call last):
  File "strg2dict.py", line 4, in <module>
    json_dict = json.load(JSON)
  File "/usr/lib/python2.7/json/__init__.py", line 291, in load
    **kw)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 22 column 5 (char 466)

I have check from others solution but still not able to find the solution. Please advise further. Thank you

@@@@@@@@@@@@@@@@@@@@@@@@@ Got comma "total_dev_log": "1423",

===> remove comma "total_dev_log": "1423"

RESOLVED> Thank you

4
  • 2
    That's not valid json. You have a trailing comma on line 21: "total_dev_log": "1423",. Commented Mar 12, 2020 at 2:49
  • The exception has told you the place of error:line 22 column 5 (char 466). Commented Mar 12, 2020 at 2:51
  • jsonviewer.stack.hu is too permissive. Use jsonlint.com, it reports the error. Commented Mar 12, 2020 at 2:52
  • I have check above said line... my mistake... thank you sir...jsonviewer.stack not detect the error Commented Mar 12, 2020 at 2:53

1 Answer 1

2

Your json file includes a trailing comma on line 21:

"total_dev_log": "1423",
                       ^ 

The JSON specification does not allow for trailing commas. Simply delete this comma to correct the error.

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

2 Comments

Thank you sir. I have corrected the line. and ok. Thank you
@chenoi Glad to here that this solved your problem. If my answer suffices, consider marking it as accepted so that this question can be removed from the unanswered question pool. See this help center topic for more info.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.