flatten-serde-json icon indicating copy to clipboard operation
flatten-serde-json copied to clipboard

Flatten arrays further

Open romilpunetha opened this issue 2 years ago • 0 comments

Currently, the array flattener merges the arrays.

{ "a" : [ "b", [ "c", "d" ]] } => { "a" : ["b", "c", "d"]}

Could we add a parameter that completely flattens the json including the arrays?

{ "a" : [ "b", [ "c", "d" ]] } => { "a.0" : "b", "a.1.0": "c", "a.1.1" : d}

The idea is to add another parameter to the flatten function to take an optional field that denotes if the arrays need to be flattened.

pub fn flatten(json: &Map<String, Value>, flatten_array: Option<bool>) -> Map<String, Value>

This would result in the following:

{
"a": [
            "b",
            ["c", "d"],
            { "e": ["f", "g"] },
            [
                { "h": "i" },
                { "e": ["j", { "z": "y" }] } 
            ],
            ["l"],
            "m"
         ]
}

=> 

{
  "a.0": "b",
  "a.1.0": "c",
  "a.1.1": "d",
  "a.2.e.0": "f",
  "a.2.e.1": "g",
  "a.3.0.h": "i",
  "a.3.1.e.0": "j",
  "a.3.1.e.1.z": "y",
  "a.4.0": "l",
  "a.5": "m"
}
            

Would you be willing to accept a PR for this?

romilpunetha avatar Nov 25 '22 13:11 romilpunetha