Forum Discussion

Sahil's avatar
Sahil
Contributor
5 years ago

Create xml segment only if condition is met

Hi,
I am trying to convert xm1 into xml2.
In order to that I have used XML Parser, Mapper,XML Formatter.
There is one logic, I need to create a segment only if value of a particular repeatable segment is either
jsonPath($, “$Z_RDGD102.IDOC.DKA1[].PAR") is equal to ‘AF’ or ‘AG’ or ‘PC’.
And part of output segment is like below:-
jsonPath($, "$Order.Data.Addresses[
].Address[*]”)

How do I achieve it, please let me know.

5 Replies

  • bojanvelevski's avatar
    bojanvelevski
    Valued Contributor

    Hi @Sahil,

    You can use mapping to do this. Here is an example:

    jsonPath($, “$Z_RDGD102.IDOC.DKA1[*].PAR”).map(x=>x == “AF” || x == “AG” || x == “PC”).toString()

    Or, if you have multiple PAR objects in DKA1, and at least one should match the condition, than you can go with this:

    $Z_RDGD102.IDOC.DKA1.filter(x=> x.PAR == “AG” || x.PAR == “AF” || x.PAR == “PC”).length>0

    Regards,
    Bojan

    • Sahil's avatar
      Sahil
      Contributor

      Hi,
      I tried 1st option but I am getting below error:-
      Failure: Unable to write to JSON-Path: $.Order.Data.Addresses, Reason: Expecting object or array for wildcard path, found: true,false,true,true,false,true, Resolution: Change the preceding path to refer to an object or array value

  • Supratim's avatar
    Supratim
    Contributor III

    @Sahil you can try with filter function. If you can share your sample input json and expected output as xml then it will be easy to resolve.