JSON Splitter removing the data

I am using JSON splitter to split the input document into smaller documents. However the splitter will remove the element on which we are performing the split functionality. I need some work around to either retain it or to be able to add it back after the split.

See example below,

Splitting this at $…IDOC and I get below as output,

image

I need the output as,

image

The root element will be dynamic and need to capture that and should be available after the split.

Any help will be appreciated.

Hi krupalibshah,

You can add fields you need in the ‘include paths’ section of the json splitter. I either pass all the fields i need if there are a couple. or i copy the data before the json splitter, include the primary key in the json splitter and use it to join the datasets again using a left join.

Do we have any leads who has done this kind of thing using expression lib or language? As we want to make this functionality general we are looking into the option to do it.

@tstack can you help me here?

Ah i missed the dynamic part, apologies my advice is useless then.

Unfortunately, the JSON Splitter snap does not expose the fully resolved path to the element being output. I’ve filed a feature request to add this functionality. In the meantime, you’ll need to use a Mapper with an expression to add the parent key name into the child object. For instance, the following expression will transform the input document to add a “parent” property to all objects with the name of the parent:

$.mapValues((v, k) => v instanceof Object ? v.extend({parent: k}) : v)

Since you’re transforming the whole input document, you’ll want to use $ as the output document.

Then, in the JSON Splitter, you can use the ‘Include scalar parents’ option to have the ‘parent’ property added into the output documents.

Thanks for the help!
However I wanted to understand this a bit more. The above expression need to be put in expression lib right?

The lib will decide what the root element based on some mapping and then add corresponding parent. Let me know if my understanding is correct?

Possible for you to help me with sample pipeline?

My problem was to be able to add the parent element dynamically, worked by using JSON generator,

{
  $root:
  {
        "IDOC" :$payload
    }
}

Got the value of “root” from expression library :slight_smile:

Thanks for the help!