Home > front end >  Hi, I am new to python and trying to convert a list into a Dictionary so I can get the value of the
Hi, I am new to python and trying to convert a list into a Dictionary so I can get the value of the

Time:08-20

import json

string = "'massage':'testing'"

json.loads(string)

But I get this error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/data/com.termux/files/usr/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/data/data/com.termux/files/usr/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/data/data/com.termux/files/usr/lib/python3.10/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

CodePudding user response:

That is not valid JSON. This works:

import json
string = '{"message": "testing"}'
print(json.loads(string))

Not sure what this has to do with converting lists to dictionaries though.

CodePudding user response:

according to the document

If the data being deserialized is not a valid JSON document, a
JSONDecodeError will be raised.

you have 2 mistake

  1. you miss the brackets in your string
  2. JSON specification - RFC7159 states that a string begins and ends with quotation mark. That mean single quoute ' has no semantic meaning in JSON and is allowed only inside a string.

result:

import json
string = '{"massage":"testing"}'
json.loads(string)
  • Related