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"
        }
    ]
};
Koko
  • 3
  • 3

1 Answers1

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)
njha
  • 1,118
  • 1
  • 13
  • 24
  • 1
    I'm not sure if I'm supposed to somehow link the answer I got this from, so I copied/pasted instead. Source: https://stackoverflow.com/questions/24027589/how-to-convert-raw-javascript-object-to-python-dictionary – njha Oct 10 '17 at 22:01
  • The ; in the end needed to be removed from my string and it will work with demjson, Thanks. – Koko Oct 11 '17 at 08:31
  • Unfortunately none of these tools can be installed anymore – Milan Jun 09 '22 at 18:47