JSON deserialization against a JSON which has nested array property

Hello,

I have a problem on JSON deserialization.
I get the problem by following procedure.

  1. 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.
  2. Use JSON Deserialize tool with the structure created step 1 in action flow.
  3. To run the action with JSON text shown in step 1.
  4. 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.


My environment info:

  • Personal Environment 11.0.118.0
  • Service Studio 11.0.406.0

Could anyone help me?

Best regards,
Moriya Takasi

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.

Takasi Moriya 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.

{
    "answers": [
        {
            "a": [
                "0.12",
                "20.12"
            ]
        }
    ]
}