How to deserialize nested json?

0
Hi, Im trying to process this nested json response from the consumed REST API. You cannot use the default Mendix REST functionality with mappings and all, because this requires your domain model to have x number of associations. And you don't know x.  So you need the deserializeJsonToObject java action from the REST module, to convert the json into Mendix objects. I have tried this, but I do not fully understand how to handle the complex attribute names, and how to use it with associations, see: https://community.mendix.com/link/questions/10452 Is it possible to use the deserializeJsonToObject java action for this, and what would the domain model look like to make this work? Cheers, Wilfried { "family" { "1" : { "id" : "1", "name" : "Jason Lengstorf", "age" : "24", "gender" : "male" }, "2" : { "id" : "2", "name" : "Kyle Lengstorf", "age" : "21", "gender" : "male" }, etc. } }  
asked
2 answers
4

While I've seen this structure in JSON before, I feel like it's an improper use of the format for the information it's trying to convey. Really this JSON is describing a family that has a list of people, and from my perspective should therefore be denoted as a list. So, what if you run a replace() function on the inbound JSON and convert it to something more proper, then import. I'm thinking you could change it to:

{
	"family": [
		{
			"id": "1",
			"name": "Jason Lengstorf",
			"age": "24",
			"gender": "male"
		},
		{
			"id": "2",
			"name": "Kyle Lengstorf",
			"age": "21",
			"gender": "male"
		}
	]
}

That would be very easy to import.

You'd have to change the curly braces for family to square brackets, then find instances of a number in quotes followed by a curly brace and remove them. I think that's all doable with Regex in the replace() function.

answered
0

Maybe this module will help you transform it: https://appstore.home.mendix.com/link/app/106051/EPI-USE-Systems/JSON-Transform ? let us know!

I'll search for a draft blog I started about processing mixed arrays, because they are not supported by the default mappings: https://docs.mendix.com/refguide/json-structures#2-2-json-arrays 

answered