-1

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
    }
Zyoumir
  • 33
  • 6
  • All the samples are invalid JSON. Are all of them arrays? How is indSap == 7 mapped since it is not in the 'table'? – aled Jul 18 '22 at 14:50
  • These are not the complete json objects, I copied a part of them for example purposes. – Zyoumir Jul 18 '22 at 14:55
  • You should provide the part of the table needed to get a consistent output then and they should be valid JSON at the least. – aled Jul 18 '22 at 14:56

1 Answers1

1

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
  }
]
aled
  • 21,330
  • 3
  • 27
  • 34
  • It actually worked thank you Aled, I did mention that null and0 get the value 1/// 1 to 5 get the value 2 /// 6 to 9 get the value 3 – Zyoumir Jul 18 '22 at 14:58
  • 1
    The explanation is confusing because it would be easy to provide a solution without using the table and the relationship to those rules was somewhat unclear. – aled Jul 18 '22 at 15:02
  • I tried my best to edit it – Zyoumir Jul 18 '22 at 15:03