How to map an JSON objects with variable names

0
Hi, For a project I need to map a JSON response (see below) in Mendix. The problems I am facing here, are the variable object names (15769769 and 15800532). The JSON tutorials tell me that the object names in a JSON response have to be the same as the association names in Mendix. I can get JSON responses with static object names working in Mendix, so for me the problem is ow to handle non-static object names in a JSON response. Does anyone has a solution for my problem? { "data": { "results": { "15769769": { "id": "15769669", "resource_id": "548235", "user_id": "7989390", "start_time": "2016-07-01 10:00:00", }, "15800532": { "id": "15805332", "resource_id": "543835", "user_id": "7986590", "start_time": "2016-07-01 11:00:00", } } } }
asked
6 answers
3

You can define the key value dynamically by using "_jsonkey". Take a look at this page in the section "Serializing complex attribute names".

answered
1

Armando,

If I understand correctly the json that you get will contain the id as an attribute but also as an object, so the json looks like:

 "123":{
          id:123, }, 
"456":{
        id: 456, }

one time and the next time the same response will look like

   "789":{
              id:789, }, 
    "111":{
            id: 111, }

So the association that the Mendix app would use is changing on every message and can't be set to a default value. In the first case the association would be 123 and 456 and in the second case it would be 789 and 111. This can;t be solved with the domain model only, I would turn to rewriting the response json so that the varaible associations are replaed with a fixed value. Replace the 111:{ and 789:{ part both with with reservation:{ with some regex, find and replace functionality and afterwards perform the serialization with the domain model containing the reservation association between result and data as in the screenshot above.

answered
0

Based on what you are saying, the keys 15800532 and 15769769 are always different, for each request? In that case it would probably be impossible to use the current functions from the REST services module. However if you have Java knowledge you should be able to use the standard deserializeJsonToObject java action functionality to built your own version that is able to handle variable JSON structure keys.

answered
0

Hi,

Thank you for the information. I am still a bit stuck, so perhaps I need to explain my problem a bit better:

JSON Response:

{"data":{  
   "results":{  
     "1576969":{  
        "id":"1576969",
        "resource_id":"54835",
        "user_id":"798990",
        "start_time":"2016-07-01 10:00:00",
        "end_time":"2016-07-01 10:59:59",
        "status":"7",
        "quantity":"1",
        "creation_date":"2016-02-22 21:09:06",
        "custom_color":null,
        "resource_name":"Lorem",
        "site_id":"21024"
     },
     "1580532":{  
        "id":"1580532",
        "resource_id":"54835",
        "user_id":"798990",
        "start_time":"2016-07-01 11:00:00",
        "end_time":"2016-07-01 11:59:59",
        "status":"7",
        "quantity":"1",
        "creation_date":"2016-02-24 13:20:17",
        "custom_color":null,
        "resource_name":"Ipsum",
        "site_id":"21024"
     }
  }}

My domain model:

Request (non persistent)

Association

Request * > 1 DataRequest (name of association is "data")

DataRequest (non persistent)

Association

DataRequest * > 1 Result (name of association is "results")

Result (non persistent)

Association

Result * > * Reservation (name of association is "reservations")

Atrributes

user_id (string)
resource_name (string)

So what do I have to change/add in the "reservations" association/the Result entity ? I know that something is wrong there, but I need a little more help what and where to do it.

What I want is JSON -> Mendix objects. And I can't get the information from the 2 objects (1576969 and 1580532) in Mendix objects.

The problem looks a lot like the one in this thread: https://forum.mendix.com/questions/9174/JSon-deserialize-2. But the solution that is offered in the thread does not work (or I am doing something wrong...).

answered
0

You would need to change the name of your association "reservations" into "_id"

BTW next time post a screenshot of your domain model that makes it a lot easier to follow it.

answered
0

Hi Mitchel,

Tnx for the answer. I will post and screenshot of the model so others understand it as well.

This is the initial situation.

model

I will replace the association "reservations" to "_id" and I will post the result (and screenshot) here later.

answered