0

I'm trying to convert a string to a dictionary but I keep getting this error:
ValueError: dictionary update sequence element #0 has length 1; 2 is required
Here's my code:

string = r'{"extra":[{"text":"Hello World"}]}'
json_data = dict(string)
1
  • 1
    You can't just load a JSON text just like that. You need the json parser: docs.python.org/3/library/json.html (particularly the loads function to decode the string into a Python dict) Commented Dec 26, 2017 at 22:32

1 Answer 1

2

I believe you are looking for:

import json
string = r'{"extra":[{"text":"Hello World"}]}'
json_data = json.loads(string)

There are a few JSON parsing libraries. Default one should suffice, simplejson is another option (just to give you an alternative).

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.