0

I have an input json which is read from a denormalized table. In my scala application, I need to normalize it before returning the resultset.

If below is the sample input:

inputJson = {
    "k1": "v0",
    "k2": [{
        "k21": [
            "v1",
            "v2",
            "v3"
        ]}, {
        "k22": [
            "v2",
            "v3",
            "v4"
        ]
    }]
}

my output json should look as follows:

outputJson =  {
    "k1": "v0",
    "k2": [{
        "k21": [1, 2, 3]
    }, {
        "k22": [2, 3, 4]
    }],
    "values": [{
        "1": "v1"
    }, {
        "2": "v2"
    }, {
        "3": "v3"
    }, {
        "4": "v4"
    }]
}

How can I achieve this in scala elegantly with/without using any json manipulation libraries

Gayatri Mahesh
  • 327
  • 1
  • 3
  • 12
  • Your wanted output is not valid json. `"k2" : ["k21":[1,2,3], "k22":[2,3,4]]` is invalid. Also... `{1:"v1"}` should be changed to `{"1": "v1"}`. To know more about json specification, look at this - http://www.json.org/ . The white sidebar on the right side is json grammer. – sarveshseri Nov 28 '16 at 13:28
  • @SarveshKumarSingh, thanks updated the json syntax as per spec – Gayatri Mahesh Nov 29 '16 at 05:21
  • Are the keys of your objects (`k1`, `k2`, `k21`) fixed or they can change? – mfirry Nov 29 '16 at 21:04

0 Answers0