2

Is there any way to parse Python list in PHP?

I have data coming from Python stored in MySQL, something like this:

[{u'hello: u'world'}]

And need to use it in PHP script. The data is a valid JSON, only difference are those leading u'

So I can replace all u' with ' and then replace all ' with " to get it into json. When I replace everything, if there is ' in the actual value, it is replaced by " as well and breaks the json.

So.. I tried a lot of stuff, but none of them was able to parse proper json thus my question: Is there any way to parse Python generated list/json-like data in PHP? I don't mind using some third-party library or etc, just want to get the data parsed...

23
  • 11
    Ideally, you should fix that Python program so that it's exporting actual JSON, using json.dumps(), rather than simply printing the repr() of the Python data structure. Commented Feb 3, 2016 at 13:04
  • 3
    The proper way is to store correctly serialized data. So, when you write data to mysql add serialization step: json.dumps(your_list_of_dicts). In this case you don't need any workarounds to parse not formalized strings anywhere. Commented Feb 3, 2016 at 13:07
  • 2
    Why is it out of your hands? No external API could possibly be returning data in this format, which means the Python script must be something you or a colleague has written. You or they should fix it. Commented Feb 3, 2016 at 13:16
  • 2
    Don't you have access to Python so you could use it to parse the data and then transform it into something PHP understands? Commented Feb 3, 2016 at 13:18
  • 3
    Quite honestly, putting it through a small Python script is the most elegant way IMO. Otherwise you'd have to replicate the Python string literal parser in PHP, which I would not attempt unless absolutely necessary. Commented Feb 3, 2016 at 13:21

1 Answer 1

4

If you have access to python, you can convert it to json from the command line. Here's an example.

$ echo "{u'key': u'value'}" |\
  python -c "import sys, json, ast; print(json.dumps(ast.literal_eval(sys.stdin.read())))"

{"key": "value"}

Here's a better formatted version of the python oneliner:

import sys, json, ast
data = ast.literal_eval(sys.stdin.read())
print(json.dumps(data))

By using ast.literal_eval instead of regular eval we can evaluate the python dictionary literal and not worry about potential code execution vulnerabilities.

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

4 Comments

eval can be dangerous to use on arbitrary data. It's far better to use ast.literal_eval for something like this.
I've changed the answer to include your suggestion.
Per stackoverflow.com/questions/9949533/…, why not just use json.loads instead of ast.literal_eval ?
@parkamark Because the input is a python literal, not json.

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.