Import Mapping Not Processing The Response Json Correctly

0
Hello Makers, I am currently engaged in a REST API integration project. The process is quite straightforward: call the API and process the response JSON using import mapping. Here is an example of what the response JSON looks like: { "data": [ { "attributes": { "detail": [ { "fieldId": "XXXXX1", "fieldType": "number", "value": 59.59 }, { "fieldId": "XXXXX2", "fieldType": "currency", "value": [ { "currency": "GBP", "value": 260000.0 } ] } ] } } ] }   You can see that there are two attributes with the same key but different types. The first "value" is a string, and the second "value" is an array. The JSON is correct because when parsed with any other online JSON parser, it recognizes both "values". However, in Mendix, it only recognizes the "value" type that appears first and ignore the one that comes after.   Has anyone encountered this problem and discovered a solution?
asked
1 answers
0

Hi Shreyash,

 

Mendix cannot handle these "dynamic" attributes, where in one instance it is a decimal value and in the next its an array of objects.

The JSON document in Mendix will show a warning when you input this JSON string, that it will only look at the first element of a given JSON string (in this case it will map the value to an integer attribute).

If you want this data imported you can map the fields that are always of the same type and the fields / objects that are not static you can map through microflow actions, e.g. use JSON Path module to load the values for these instances or use other marketplace modules aimed at mapping dynamic JSON documents.

Another approach would be to first modify the JSON into a document that has a fixed structure.

 

Edit 202409101117:

- You need to store the http content in the httpRequest (or string) so you can use that string for futher processing (the Import mapping & JSON paths)

- Apply additional mappings per scenario. In this example I store the result on an attribute (e.g. write a section of the JSON into an attribute and depending on the type you can add additional import mappings specific for that section)

- Use https://jsonpath.com/ to check what json paths you need

image.png

 

With kind regards,

Stephan

answered