Search code examples
arraysobjectsnaplogic

How do I turn array items into an object in SnapLogic


In SnapLogic, I have an input JSON like the one below, it appears to be an array. I am trying to put it into a structure where it is an object called "remitLocations" composed of an array of addresses containing the following items. I have been trying to use mapper and structure snaps to do this. I also tried using JSON and XML generators to give it the structure I want, but so far nothing has worked.

[
  {
    "addressInternalid": 2631363,
    "isDelete": false,
    "internalSupplierid": 3423589,
    "acctGrpid": "RT",
    "address1": "5309 GREENWAY",
    "address2": "5301 REDWAY",
    "address3": "5504 BLUEWAY",
    "poBox": "0912KHJWD",
    "country": "USA",
    "state": "US-TX",
    "city": "FREE",
    "zip": "78211",
    "phone": "2229808888",
    "phoneExtn": "091",
    "fax": "747",
    "faxExtn": "737",
    "fullCompanyName": "SUPPLIER MARCH 3 dba TEXT",
    "requestId": 5272423,
    "id": "3423589",
    "facilityCode": "0001",
    "systemCode": "1",
    "supplierType": "Operational",
    "status": "ACTIVE"
  },
  {
    "addressInternalid": 2631367,
    "isDelete": false,
    "internalSupplierid": 3423589,
    "acctGrpid": "RT",
    "address1": "11305 4 PTS DR",
    "address2": "BLDG 2,#100",
    "country": "USA",
    "state": "US-TX",
    "city": "AUSTIN",
    "zip": "78726",
    "phone": "5126648805",
    "phoneExtn": "123",
    "fax": "123",
    "faxExtn": "134",
    "fullCompanyName": "SUPPLIER MARCH 3 dba TEXT",
    "requestId": 5272423,
    "id": "3423589",
    "facilityCode": "0001",
    "systemCode": "1",
    "supplierType": "Operational",
    "status": "ACTIVE"
  },
  {
    "addressInternalid": 2631368,
    "isDelete": false,
    "internalSupplierid": 3423589,
    "acctGrpid": "RT",
    "address1": "REMIT 11305 4 PTS DR",
    "address2": "BLDG 3",
    "country": "USA",
    "state": "US-TX",
    "city": "AUSTIN",
    "zip": "78725",
    "phone": "5126600000",
    "phoneExtn": "678",
    "fax": "678",
    "faxExtn": "678",
    "fullCompanyName": "SUPPLIER MARCH 3 dba TEXT",
    "requestId": 5272423,
    "id": "3423589",
    "facilityCode": "0001",
    "systemCode": "1",
    "supplierType": "Operational",
    "status": "ACTIVE"
  }
]

Solution

  • Note: Answer contains two solutions. The bottom one seems to be a better solution because you use only one snap.


    Solution #1

    Use JSON Formatter to aggregate the incoming documents into a single JSON. Then use a Binary to Document Snap with encoding/decoding set to None. After that, you can just parse the content and put it under any field name in a mapper.

    Sample Pipeline:

    Sample pipeline

    Binary to Document:

    binary to document

    Final Mapper:

    parsing json

    Output:

    output

    Notes

    • No configuration was changed in the JSON Formatter
    • Setting encoding/decoding to None in the Binary to Document Snap is important
    • Parse the content like - JSON.parse($content)

    Solution #2

    There is actually a better way to do it - Use the "Group By N Snap with Target Field as remitLocations and Group Size as 0.

    A value of 0 instructs the Snap to group all input documents into a single document.

    Sample Pipeline

    enter image description here

    Group By N

    enter image description here

    Output

    enter image description here