Forum Discussion

manohar's avatar
manohar
Contributor
4 years ago

Array vs Non Array

hi there, I have 2 json files. one has the array and the other one has non-array child-structure.

The parent is also array. At the parent level I get individual records but when it comes to Child, if its non-array, then it mashes up the childs for each of the parent. it works fine if the child is also array.

Please find below the 2 files. E1EDPT2 is the child thats causing issue.

I am also attaching my pipeline one for the array and the other for non-array one. both having same mapping.

Can anyone please check and let me know where I m doing wrong?

Any help is greatly appreciated.

Thanks
Manohar
for_Non-Array_Text.slp (4.7 KB)
for_Array_Text.slp (5.2 KB)

non-array

[
  {
    "LINES": [
      {
        "POSEX": "00010",
        "E1EDPT1": {
          "E1EDPT2": {
            "TDLINE": "Line 10 non-array text"
          }
        }
      },
      {
        "POSEX": "00020",
        "E1EDPT1": {
          "E1EDPT2": {
            "TDLINE": "Line 20 non-array text"
          }
        }
      },
      {
        "POSEX": "00030",
        "E1EDPT1": {
          "E1EDPT2": {
            "TDLINE": "Line 30 non-array text"
          }
        }
      }
    ]
  }
]

array text

[
  {
    "LINES": [
      {
        "POSEX": "00010",
        "E1EDPT1": {
          "E1EDPT2": [
            {
              "TDLINE": "This is the First array text being sent."
            },
            {
              "TDLINE": "This is the second line of First array text being sent"
            }
          ]
        }
      },
      {
        "POSEX": "00020",
        "E1EDPT1": {
          "E1EDPT2": [
            {
              "TDLINE": "This is the second array text being sent."
            },
            {
              "TDLINE": "This is the second line of second array text being sent"
            }
          ]
        }
      },
      {
        "POSEX": "00030",
        "E1EDPT1": {
          "E1EDPT2": [
            {
              "TDLINE": "This is the third array text being sent."
            },
            {
              "TDLINE": "This is the second line of third array text being sent"
            }
          ]
        }
      }
    ]
  }
]

6 Replies

  • Hello @manohar,

    If you look at the Mapper screenshot below, is the expression sl.ensureArray(jsonPath($, "$LINES[*].E1EDPT1.E1EDPT2")) accomplishing what you want? It turns “E1EDPT2” into an array if it isn’t already one, then either in the same line or in a follow up Mapper snap you would run the next bit of processing. Let us know if that helps.

  • @manohar You’re welcome, happy to help! I made a mistake with my initial pipeline with the expression sl.ensureArray(jsonPath($, "$LINES[*].E1EDPT1.E1EDPT2")), which ended up replicating the “TDLINE” items to every “POSEX” and ultimate leading to the merged string you observed.

    I updated my pipeline to use a JSON Splitter to break “LINES” up first before performing sl.ensureArray($E1EDPT1.E1EDPT2) - effectively doing the same thing we did before but with no replication of “TDLINE.” Here is a screenshot of the pipeline:

    Here is the final output of the “Construct Orderlines” Mapper Snap:

    Also, since we’re using sl.ensureArray($E1EDPT1.E1EDPT2) to make sure “E1EDPT2” is an array, I also cleaned up your Mapper expression to remove the ternary expression that used typeof to determine if it was an array or object. The updated expression looks like this:
    $LINES.map(x => {"line-num": x.POSEX,"attachments":{'type':"AttachmentText",'intent':"Supplier",'text': Date.parse(Date.now()).toLocaleDateTimeString({"timeZone":"US/Eastern", "format":"yyyy-MM-dd HH:mm"}) + " :- " + x.E1EDPT1.E1EDPT2.map(y=>y.TDLINE).toString().replace(/\,/g,"").replaceAll("<br/>","\n").replaceAll('], [','\n').replaceAll('[','').replaceAll(']', '')}})

    Please let us know if this fully addresses your question or if not, where does this fall short.

    Here is the pipeline:
    Community_ArrayVsNonArray_2021_07_29.slp (12.4 KB)

  • @rsramkoski thanks for that.

    but when I try that, I get it as below

    and at the end I still get it as

    my end structure should come as below

    [
      {
        "order-lines": [
          {
            "line-num": "00010",
            "attachments": {
              "type": "AttachmentText",
              "intent": "Supplier",
              "text": "2021-07-29 20:08 :-  Line 10 change Item text"
            }
          },
          {
            "line-num": "00020",
            "attachments": {
              "type": "AttachmentText",
              "intent": "Supplier",
              "text": "2021-07-29 20:08 :-  Line 20 change Item text"
            }
          },
          {
            "line-num": "00030",
            "attachments": {
              "type": "AttachmentText",
              "intent": "Supplier",
              "text": "2021-07-29 20:08 :-  Line 30 change Item text"
            }
          }
        ]
      }
    ]
    

    hope that helps.

    Thanks
    Manohar