Skip to content

How to convert PyDict to serde_json::Value? #4625

Answered by LilyFoote
0x3E6 asked this question in Questions
Discussion options

You must be logged in to vote

You could try this:

        let x = Python::with_gil(|py| {
            let locals = PyDict::new_bound(py);
            Python::run_bound(py, r#"
import json
a= json.dumps({"a":1, "b":2})
"#, None, Some(&locals)).unwrap();
            let ret = locals.get_item("a").unwrap().unwrap();
            let s = ret.extract::<String>().unwrap();
            DataValue::Json(serde_json::from_str(s.as_str()).unwrap())
        }
        );
        println!("result: {:?}", x);

I haven't tested this myself, so you may need to tweak it a bit.

You could also look into the pythonize crate, which might work for you.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@0x3E6
Comment options

Answer selected by 0x3E6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants