How to handle json object with dynamic key name

1
Hi All, I have a json object as below:    "ServiceData": {         "modelObjects": {             "abc": {                 "uid": "abc",                 "className": "className"             },            "abc2": {                 "uid": "abc2",                 "className": "className"             }         }   The keys "abc" and "abc1" are dynamic. Below is my domain model   I referred few posts from the forum but could not find the information on how can I handle this dynamic key. Because when I get the response from REST call, the keys are different than what I have specified in my json, so it does not create any object under ModelObjects. Below is my Import-mapping   I am not sure what key should I mention in my json and how to retrieve it at rumtime.   Can anyone please help?   Thanks, Pallavi
asked
2 answers
1

If the attributes of the object with the dynamic name are always the same, you could try to return the json string in your REST call, do a manual replacement, so that the names are always the same. And then apply the mapping.

Maybe there are better solutions, but that's my first thought.

answered
0

The property names "abc" and "abc2" are not known in advance, so they can't be used in an import mapping.

There are two solutions:

1 - Can you change the JSON format? If modelObjects were an array instead of an object, you could parse i. The property values are available in the uid property.

"ServiceData": {
        "modelObjects": [
            {
                "uid": "abc",
                "className": "className"
            },

            {
                "uid": "abc2",
                "className": "className"
            }
        ]
}

2 - If you can't change the JSON structure, you need a custom java action that converts the json into the format above.

answered