2

I have a string of the following form:

"[{'status': 'Unshipped', 'city': 'New Delhi', 'buyer_name': 'xxx', 'name': 'xxx', 'countryCode': 'IN', 'payment_method': 'COD', 'y_id': 'xxx', 'phone': 'xxx', 'state': 'New Delhi', 'service': 'Expedited', 'amout': '425.00', 'address_1': 'xxx', 'address_2': 'xxx', 'postalCode': '110018', 'shipped_by_y': 'false', 'channel': 'MFN', 'order_type': 'StandardOrder'}, {'status': 'Unshipped', 'city': 'thane', 'buyer_name': 'xxx', 'name': 'xxx', 'countryCode': 'IN', 'payment_method': 'COD', 'y_id': 'xxx', 'phone': 'xxx', 'state': 'Maharashtra', 'service': 'Expedited', 'amout': '350.00', 'address_1': 'xxx', 'address_2': 'xxx', 'postalCode': '400607', 'shipped_by_y': 'false', 'channel': 'MFN', 'order_type': 'StandardOrder'}]\n"

How can I convert this string to an array of corresponding dictionaries.

I tried to convert the string to JSON and then loading the JSON. But it remains a string.

Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
asked May 30, 2014 at 11:34
6
  • 3
    And how did you convert it to JSON then, for it to remain a string when you loaded it? Can you show us a real example rather one made up? Are there no quotes around the keys and values, for example? Commented May 30, 2014 at 11:36
  • @MartijnPieters : I did a json.dumps followed by json.loads Commented May 30, 2014 at 11:36
  • If the string is valid JSON, try to only use loads. Commented May 30, 2014 at 11:39
  • @nish: This isn't valid JSON. This looks like a Python string instead. Commented May 30, 2014 at 11:41
  • 1
    @nish: Then show us that code. You didn't here; JSON uses double quotes. Perhaps you dumped output of str()? Commented May 30, 2014 at 11:49

1 Answer 1

9

You don't have JSON there; that looks like you converted a Python list with dictionaries to string directly.

Use ast.literal_eval() to convert it back to Python objects:

import ast
obj = ast.literal_eval(datastring)

ast.literal_eval() limits parsing to just object literals (tuples, lists, dictionaries, strings, numbers), avoiding the arbitrary code execution risks that a full-blown eval() call carries with it.

Demo:

>>> import ast
>>> datastring = "[{'status': 'Unshipped', 'city': 'New Delhi', 'buyer_name': 'xxx', 'name': 'xxx', 'countryCode': 'IN', 'payment_method': 'COD', 'y_id': 'xxx', 'phone': 'xxx', 'state': 'New Delhi', 'service': 'Expedited', 'amout': '425.00', 'address_1': 'xxx', 'address_2': 'xxx', 'postalCode': '110018', 'shipped_by_y': 'false', 'channel': 'MFN', 'order_type': 'StandardOrder'}, {'status': 'Unshipped', 'city': 'thane', 'buyer_name': 'xxx', 'name': 'xxx', 'countryCode': 'IN', 'payment_method': 'COD', 'y_id': 'xxx', 'phone': 'xxx', 'state': 'Maharashtra', 'service': 'Expedited', 'amout': '350.00', 'address_1': 'xxx', 'address_2': 'xxx', 'postalCode': '400607', 'shipped_by_y': 'false', 'channel': 'MFN', 'order_type': 'StandardOrder'}]\n"
>>> ast.literal_eval(datastring)
[{'status': 'Unshipped', 'city': 'New Delhi', 'buyer_name': 'xxx', 'name': 'xxx', 'countryCode': 'IN', 'payment_method': 'COD', 'shipped_by_y': 'false', 'phone': 'xxx', 'state': 'New Delhi', 'service': 'Expedited', 'address_1': 'xxx', 'address_2': 'xxx', 'postalCode': '110018', 'order_type': 'StandardOrder', 'amout': '425.00', 'channel': 'MFN', 'y_id': 'xxx'}, {'status': 'Unshipped', 'city': 'thane', 'buyer_name': 'xxx', 'name': 'xxx', 'countryCode': 'IN', 'payment_method': 'COD', 'shipped_by_y': 'false', 'phone': 'xxx', 'state': 'Maharashtra', 'service': 'Expedited', 'address_1': 'xxx', 'address_2': 'xxx', 'postalCode': '400607', 'order_type': 'StandardOrder', 'amout': '350.00', 'channel': 'MFN', 'y_id': 'xxx'}]
answered May 30, 2014 at 11:43
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.