JSON deserialization against a JSON which has nested array property
950
Views
8
Comments
Solved
Question
Hello,
I have a problem on JSON deserialization.
I get the problem by following procedure.
Create a structure by using 'Add structure from JSON' with following JSON text.
{
"answers": [
"0.12",
"20.12"
}
Note that this is a simplified JSON from my project's much more complicated JSON.
Use JSON Deserialize tool with the structure created step 1 in action flow.
To run the action with JSON text shown in step 1.
The error message below will appear.
Failed to deserialize JSON to MyAnswer:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ssSvgDesignViewer.RestRecords.JSONSTAnswerItemStructure' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path 'answers[0]', line 3, position 9.
Hello,
Thank you for introducing the component.
I'm able to build preprocessor with this component in OnAfterResponse.
Writing JSONata transform is easier than making preprocessor by myself.
Thanks a lot.
Hi Moriya,
To what kind of OutSystems data structure do you want to deserialize this? Since the Platform doesn't have a concept of Arrays, the translate to Lists, but Lists of unnamed Lists don't exist on the Platform.
Hello Kilian,
Thank you for replying.
I am attempting to use a structure made by 'Add structure from JSON' context menu with JSON I wrote above.
Hello Takasi,
I slightly modified your input json to the below and now am able to deserialize . Still looking into the issue.
{
"answers": [
{
"test": [
{
"name": "0.12"
},
{
"name": "20.12"
}
]
}
]
}
Check the below post on stackoverflow
https://stackoverflow.com/questions/33755928/cannot-deserialize-the-current-json-array-e-g-1-2-3
Regards
Amal
Hello Amal,
Thank you for your comment.
The JSON I wrote above was completely simplified one to explaining problem. Original JSON I am treating in my project is very large and is much more complicated. And the original JSON file is provided from other system which is outside of OutSystems application.
To apply your modification to my original JSON I have to do complex text processing. If complex text processing is needed, I will do manual deserialization in the text processing and not use JSON deserialize tool of OutSystems. I want to avoid doing manual complex process which often causing bugs.
wrote:
Hello Amal,
Thank you for your comment.
The JSON I wrote above was completely simplified one to explaining problem. Original JSON I am treating in my project is very large and is much more complicated. And the original JSON file is provided from other system which is outside of OutSystems application.
To apply your modification to my original JSON I have to do complex text processing. If complex text processing is needed, I will do manual deserialization in the text processing and not use JSON deserialize tool of OutSystems. I want to avoid doing manual complex process which often causing bugs.
Hi Takasi,
I tried with ArdoJson extension but that does not support your sample json.
Maybe you could clone it and write your own deserialization logic.
Looking forward to see how this gets solved.
Regards
Amal
I think the problem lies within the fact that you have an array, with an array of 2 values.
I'm not sure what your target is, but it would make more sense to have an array of objects, which have a number of properties. Which you are doing above, you could also encapsulate it within an object instead.
Please use JSONata forge component . IT will transform the Json . just capture the response in on After response and transfor your json by using this component . It will work
https://www.outsystems.com/forge/component-overview/12401/jsonata-transformation
Hello,
Thank you for introducing the component.
I'm able to build preprocessor with this component in OnAfterResponse.
Writing JSONata transform is easier than making preprocessor by myself.
Thanks a lot.