0

I'm trying to find a parser for python that can parse the data structure (to a python dictionary) that is written below (this data structure was taken from var variable in javascript).

{
    a: "a",
    b: 54,
    c: [
        {
            d: "d",
            e: false
        },
        {
            f: "f"
        }
    ]
};
2
  • You could probably use json, which is Javascript-Object-notation after all... Commented Oct 10, 2017 at 21:47
  • Hmm, actually, no you couldn't, because a, b etc would have to be strings. Commented Oct 10, 2017 at 21:51

1 Answer 1

0

demjson.decode()

import demjson

# from
js_obj = '{x:1, y:2, z:3}'

# to
py_obj = demjson.decode(js_obj)

jsonnet.evaluate_snippet()

import json, _jsonnet

# from
js_obj = '{x:1, y:2, z:3}'

# to
py_obj = json.loads(_jsonnet.evaluate_snippet('snippet', js_obj))

ast.literal_eval()

import ast

# from
js_obj = "{'x':1, 'y':2, 'z':3}"

# to
py_obj = ast.literal_eval(js_obj)
Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure if I'm supposed to somehow link the answer I got this from, so I copied/pasted instead. Source: stackoverflow.com/questions/24027589/…
The ; in the end needed to be removed from my string and it will work with demjson, Thanks.
Unfortunately none of these tools can be installed anymore

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.