Flat nested JSON to header level using scala

2019-08-19 00:09发布

问题:

Below is my sample JSON and can be nested to any level deep:

{
    "key1": {
        "keyA": 'valueI'
    },
    "key2": {
        "keyB": 'valueII'
    },
    "key3": [
    {
    "a":1,
    "b":2
    },
    {
    "a":1,
    "b":2
    }
    ]
}

This shloud be splitted up into 2 JSONs as Key3 has 2 array elements. Output should be like this:

JSON1 =
{
"key1_keyA":'valueI',
"key2_keyB":'valueII',
"key3_a":1,
"key3_b":2
}
JSON2=
{
"key1_keyA":'valueI',
"key2_keyB":'valueII',
"key3_a":1,
"key3_b":2
}

I am getting this kind of JSON from source and reading it from SPARK framework using scala.