Forum Discussion

manohar's avatar
manohar
Contributor
4 years ago
Solved

Sort internal array

hi there,

I have a inbound json thats coming this way.

How can I sort the Order-Lines array based on line-num in asscending order. tried to use sort but did not work.

any other solutions?

Thanks
Manohar

[
  {
    "order-lines": [
      {
        "line-num": "00010",
        "attachments": {
          "type": "AttachmentText",
          "intent": "Supplier",
          "text": "2021-09-02 17:35 :-  Blade #60272"
        },
        "need-by-date": "20210901"
      },
      {
        "line-num": "00030",
        "attachments": {
          "type": "AttachmentText",
          "intent": "Supplier",
          "text": "2021-09-02 17:35 :-  Blade # 30050"
        },
        "need-by-date": "20210901"
      },
      {
        "line-num": "00020",
        "need-by-date": "20210901"
      },
      {
        "line-num": "00040",
        "need-by-date": "20210901"
      },
      {
        "line-num": "00050",
        "need-by-date": "20210901"
      }
    ],
    "currency": {
      "code": "USD"
    },
    "custom-fields": {
      "buyer-type-req": "",
      "required-signature": {
        "external-ref-code": "3"
      }
    },
    "payment-term": {
      "code": "ZN45"
    },
    "po-number": "iuiuii989"
  }
]
  • Hi @manohar,

    Use the following expression in a mapper:

    $['order-lines'].sort((a,b) => a['line-num'] - b['line-num'])

    Regards,
    Bojan

1 Reply

  • Hi @SL12345,

    Just use a JSON Splitter and split on $CarDetails array, but make sure you have enabled “Include scalar parents” option in the JSON Splitter.

    Result:

    • SL12345's avatar
      SL12345
      New Contributor III

      thank you, that is really what i wanted 🙂 and is it possible to wrap this content into object so from this:

      {
        "CompanyName": "TestCompany",
        "CompanyAddress": "TestAddress",
        "Brand": "BMW",
        "Year": "2020",
        "Fuel": "Petrol"
      }
      ,
      {
        "CompanyName": "TestCompany",
        "CompanyAddress": "TestAddress",
        "Brand": "Jaguar",
        "Year": "2020",
        "Fuel": "Diesel"
      }
      

      create this? Adding “Cars” as header or envelope for body/content?

      {
      	"Cars":
      {
        "CompanyName": "TestCompany",
        "CompanyAddress": "TestAddress",
        "Brand": "BMW",
        "Year": "2020",
        "Fuel": "Petrol"
      }
      ,
      {
        "CompanyName": "TestCompany",
        "CompanyAddress": "TestAddress",
        "Brand": "Jaguar",
        "Year": "2020",
        "Fuel": "Diesel"
      }
      }
      
      • JensDeveloper's avatar
        JensDeveloper
        Contributor II

        Hi @SL12345 ,

        Have you tried putting a groupByN snap behind it and as target field $Cars.

        Regards

        Jens