0

Before any one marks this as duplicate, i've already checked this question: Remove duplicates and merge JSON objects. (copied example from there)

I'm working in on a case that involves merging json like in the example below,

[{
        "id" : 1,
        "name" : "abc",
        "nodes" :[
            {
                "nodeId" : 20,
                "nodeName" : "test1"
            }
        ]
    },
    {
        "id" : 1,
        "name" : "abc",
        "nodes" :[
            {
                "nodeId" : 21,
                "nodeName" : "test2"
            }
        ]
    }]

to something like till the leaf nodes,

 [{
        "id" : 1,
        "name" : "abc",
        "nodes" :[
            {
                "nodeId" : 20,
                "nodeName" : "test1"
            },
            {
                "nodeId" : 21,
                "nodeName" : "test2"
            },
        ]
    }]

I can use the brute force method by recursively comparing nodes and merging manually into an object and finally converting to a json string. I was wondering if there is a better way to do it using json4s. More context, It's a scala project, there are 1000s of json that are to be merged into one and they may have nested complext structes like array and object.

Merging here means adding to pre existing data on duplicate nodes not overwriting them, as in example above.

I've also considered using trees, but not much progress there.

2
  • You need to be more clear about the processing that you want doing. What do you mean by "duplicate nodes" and "merging". What data must be the same for two nodes to be duplicates? What data gets merged and how? What if two top-level objects are identical? Commented Aug 29, 2018 at 15:31
  • duplicate nodes means common keys on same depth level. merge strategy is shown in example. merging is supposed to happen from root to end nodes. if top key is common, it is kept and child nodes are checked Commented Aug 29, 2018 at 16:15

1 Answer 1

0

You can use the merge operation:

  def main(args: Array[String]): Unit = {

    val json = """[{
                 |        "id" : 1,
                 |        "name" : "abc",
                 |        "nodes" :[
                 |            {
                 |                "nodeId" : 20,
                 |                "nodeName" : "test1"
                 |            }
                 |        ]
                 |    },
                 |    {
                 |        "id" : 1,
                 |        "name" : "abc",
                 |        "nodes" :[
                 |            {
                 |                "nodeId" : 21,
                 |                "nodeName" : "test2"
                 |            }
                 |        ]
                 |    }]""".stripMargin

    val jsonArray = parse(json).asInstanceOf[JArray]
    val res = jsonArray.arr.reduceLeft{(x,y) => x merge y}
    println(pretty(render(res)))

  }

which output:

{
  "id":1,
  "name":"abc",
  "nodes":[{
    "nodeId":20,
    "nodeName":"test1"
  },{
    "nodeId":21,
    "nodeName":"test2"
  }]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.