Nested escaped JSON to Mendix Object

Hi there, I am currently facing an issue where I have the following value in a JSON payload: { "extras": "{\"zolltarif_de\":\"v2\",\"zolltarif_ch\":\"v1\",\"single_line\":{\"destructive_class\":7,\"location_type\":\"Location type\",\"compartment\":\"Cool Pouch\",\"pack_instructions\":\"Pack instructions\"}}", "updated_at": "2020-03-20T14:03:53.245017Z", "created_at": "2020-03-20T14:03:53.245017Z" } I need to map it to a Mendix object, this is not currently supported by default from the Mendix rest implementation (from what I can tell).  Is there an existing solution to map this data out to non persistent obejcts, or is the only way to map this to a single attribute and then run transformation on it? The other issue is that this field called extras can be dynamically changed based on what is required, is there any way to facilitate this in Mendix? Kind regards, Bertram
1 answers

I think you have 2 challenges here so let’s break them down:

Nested JSON

In your rest call and import mapping, you should import the “extras” field as a string. This is probably what Mendix does by default. Then:

  • Create a JSON snippet for the nested JSON
  • Create an import mapping for the nested JSON
  • Use an “Import from Mapping” activity in your microflow to map the JSON string to Mendix objects.


Dynamic JSON

This one is tougher. If those fields change, how will you use them in any structured manner?

  • If there is a set number of fields that might be exposed, you can simply create a JSON snippet that contains all possible fields, and Mendix will only map the available fields into an object.
  • If the fields aren’t structured at all, then they won’t be useful for business logic. In that case I assume you’d just want to display them on the screen. For that, I’d write a JSON mapping Java action that creates a list of Mendix objects that have 2 attributes like “key” and “value”. Then you could use a List View to display those key-value pairs on a page.