Search code examples
muledataweavemulesoftanypoint-studio

mapping payload with json object in dataweave


What I'm trying to do is map the CodeOT to each object in my payload such as :

null/0 get the value 1 1 to 5 get the value 2 and 6 to 9 get the value 3

I am lost in how to do it as I'm new to dataweave

example of payload :

    {
    "refSig" : "0110443372",
    "indSap":2
    },
    {
    "refSig" : "0000443942",
    "indSap":0
    },
    {
    "refSig" : "0117243942",
    "indSap":null
    }

the conversion table is provided and must be used as is, here's a part of it

{
    "CodeSap": null,
    "Libelle": "",
    "CodeOT": 1
  },
  {
    "CodeSap": 0,
    "Libelle": "Elle a demandé un délai de paiement",
    "CodeOT": 1
  },
  {
    "CodeSap": 1,
    "Libelle": "Elle a des factures SATD",
    "CodeOT": 2
  },
  {
    "CodeSap": 2,
    "Libelle": "Elle a des factures remises à l’huissier",
    "CodeOT": 2
  }

I need to map the CodeOT from the conversion table to the indSap from the payload using CodeSap

I started doing this but it doesn't seem to lead me anywhere

%dw 2.0
output application/json
---

lignesOK : payload map (item, index) -> {
      bf: item mapObject (value, key) -> {
      (key): value,
      codeOt: varTable map (it,val) ->{
          (val):(it)
      }
      }
}

expected output for the example of the payload above is :

{
    "refSig" : "0110443372",
    "CodeOT":2
    },
    {
    "refSig" : "0000443942",
    "CodeOT":1
    },
    {
    "refSig" : "0117243942",
    "CodeOT":1
    }

Solution

  • Assuming that the input and table are arrays and that entries in the table are unique per CodeSap the following script works, though the output is a bit different than expected because of the incomplete table provided:

    %dw 2.0
    output application/json
    var varTable=[{
        "CodeSap": null,
        "Libelle": "",
        "CodeOT": 1
      },
      {
        "CodeSap": 0,
        "Libelle": "Elle a demandé un délai de paiement",
        "CodeOT": 1
      },
      {
        "CodeSap": 1,
        "Libelle": "Elle a des factures SATD",
        "CodeOT": 2
      },
      {
        "CodeSap": 2,
        "Libelle": "Elle a des factures remises à l’huissier",
        "CodeOT": 2
      }]
    ---
    payload map (item, index) -> {
          
          refSig: item.refSig,
          codeOt: (varTable filter (item.indSap == $.CodeSap))[0].CodeOT
    }
    

    Output:

    [
      {
        "refSig": "0110443372",
        "codeOt": null
      },
      {
        "refSig": "0000443942",
        "codeOt": 1
      },
      {
        "refSig": "0117243942",
        "codeOt": 1
      }
    ]