Published on InterSystems Developer Community (https://community.intersystems.com)

Home > Question about combine/merge json files

Question
Menno Voerman · Sep 20, 2021

Question about combine/merge json files

Hi All,

I would like to merge json files. From a BPL we're running an operation to get data from a REST service cursors. We would like to run the REST calls until no cursor is present in the response of the rest call.

This works fine, but I would like to combine/merge the json outcome and write it to the disk.

 

See my attempt below for merging 2 json files.

 

Class Test.TestClass
{

ClassMethod xxx() As %Status
{
    Set tSc    = $$$OK
    
    set jsonObj1 = {}.%FromJSON("C:\temp\1.json")
    set jsonObj2 = {}.%FromJSON("C:\temp\2.json")
    
    Do jsonObj1.%Remove("NextCursor")
    Do jsonObj2.%Remove("NextCursor")
    
    set obj2 = jsonObj2.%Get("OptOuts")
    
    Do jsonObj1.%Set("", obj2)

    Set objString = jsonObj1.%ToJSON()
    set formatter = ##class(%JSON.Formatter).%New()
    Do formatter.FormatToStream(jsonObj1, .Streamobj)
    
    Set ResponseStream = ##class(%Stream.FileBinary).%New()
    Set ResponseStream.Filename = "C:\temp\3.json"
      Do ResponseStream.CopyFrom(.Streamobj)
    Set tSC = ResponseStream.%Save()
    Do ResponseStream.%Close()    
    return tSc
}

}

This gives an outcome but there's an extra node created, see 3.json. I think this make sense if i check rule with  " Do jsonObj1.%Set("", obj2)".

Any advise is welcome how to solve this.

See atttached, 1.json, 2.json

3.json = current result

4.json = wanted result

#InterSystems IRIS for Health
Product version: IRIS 2020.1
$ZV: IRIS for Windows (x86-64) 2020.1 (Build 215U) Mon Mar 30 2020 20:14:33 EDT [HealthConnect:2.1.0]

Source URL:https://community.intersystems.com/post/question-about-combinemerge-json-files