Search code examples
jsonrustserializationrust-polars

Serialize Polars `dataframe` to `serde_json::Value`


In Polars, it is simple enough to serialize a DataFrame to a json string: JsonWriter::new(dest).finish(&df)?.

Is it possible to serialize to a json Value — a serde_json::Value — that is the Value::Array of records? Obviously, I could write the json to an in-memory string, then deserialize it into a serde_json::Value. But I'm wondering if there is a direct path from DataFrame to serde_json::Value.

This would be the rough equivalent of pandas df.to_dict(orient="records").


Solution

  • You need to add the serde feature.

    cargo add polars --features serde
    
    use polars::prelude::*;
    
    fn main() {
        let df = df! {
            "a" => [1,2,3,4,5],
        }
        .unwrap();
        let df_json = serde_json::to_value(&df).unwrap();
        println!("df_json {df_json}");
    }
    

    Note, Polars implements a custom de/serialize, and it may not align with the pandas equivalent mentioned.

    The JSON result from above:

    {
      "columns": [
        {
          "datatype": "Int32",
          "name": "a",
          "values": [1,2,3,4,5]
        }
      ]
    }