1

I have this simplify array to be converted into JSON. For some reason, it does not working.

import json
arr = "[{u'symbol': u'YTLREIT', u'code': u'5109', u'name': u'YTL HOSPITALITY REIT'}]"
data = json.loads(arr)

The error I received,

ValueError: Expecting property name: line 1 column 3 (char 2)

1
  • Where is this data coming from? Commented Jan 31, 2016 at 4:19

1 Answer 1

3

This is not a valid JSON, but you can load with ast.literal_eval():

>>> from ast import literal_eval
>>> literal_eval(arr)
[{u'symbol': u'YTLREIT', u'code': u'5109', u'name': u'YTL HOSPITALITY REIT'}]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. It works! It looks like a valid JSON. What makes it not a valid JSON?
@geckob the extra u sign before the quotes and the quotes themselves - they have to be double quotes. It actually looks like a string representation of a list. If you have control over the tool produced it, you should tweak it to produce a valid JSON - if Python, json.dump or json.dumps.
@alecxe What is the reason behind putting double quote ? why not single quote?
@NamitaMaharanwar: That's just JSON syntax. Strings must be delimited with double quotes.

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.