I used the JSON Serialize step to create a JSON string with all of my projects in it. The string looks like this:
[{"Project":{"Id":1,"Name":"First Project"}},{"Project":{"Id":2,"Name":"Second Project"}}]
In short, this is what Outsystems gives me when I ask it to JSON serialize every record in my Project table. I expect the the desialize step to be able to take this exact string and be able to turn it back into project records.
However, I get an error: "Syntax error caused by unexpected '[' element in expression." If I were to pare this down to a single project record and get rid of the brackets, I get: "Syntax error caused by unexpected ':' element in expression." I know this can't be right because JSON uses colons all the time.
Why don't the JSON Deserialize step take a valid JSON string and turn it into records for a known structure?
Hi Terry,
because you are sending a text parameter, your JSON string should be:
"[{'Id':'1','Name':'First Project'},{'Id':'2','Name':'Second Project'}]"
Regards
Hello terry,
Agree with Jose, adding to that point you will go through this post as well in that you will get hint. They are also giving JSON "[ your JSON]" like this
https://www.outsystems.com/forums/discussion/71057/failed-to-deserialize-json/
Thanks and Regards,
Akshay Deshpande
Hey Terry Weatherstone ,
Please find below attachment .I had created json like you mentioned And also deserialized into object.
To achieve this I had created structure and to convert json string in object used same structure variable.
please check this sample demo link. samplelist (personal-dt37vy6p.outsystemscloud.com)
just have a look and let me know if it fit for your expectation.
Thanks
Yogesh
Interesting. Thank you all for your help.
An interesting thing I discovered. There are actually two problems here. First, as José Gonçalves pointed out, the string has to be encapsulated in quotes. So my original string would not work. Second, the JSON string itself has to be modified. All the double quotes " have to be changed to single quotes ' .
In short the JSON string that is generated in the JSON serialize step cannot be directly used in the JSON deserialize step. You have to go in and replace all the double quotes with singles.
Not quite true Terry.
The string you see while debugging has those quotes. But i am quite sure that all serialized object can be deserialized without changing anything, if you use the correct (same) structure.
Yes, Stefano is right. The strings you see when debugging are represented the way you would if you'd assign a Text variable. This means that all double quotes are represented as double double quotes, so a text like:
He said "hello"
would be represented as:
"He said ""hello"""
This is imho a slightly confusing aspect of the debugger, but easy to get used to.
You do not need to convert anything after a JSON serialize - the content can be directly deserialized, if needed.
There's some confusion here, including in the answers, so let me try to explain what goes on here.
First, the error in your image is caused by the JSON String input parameter of the JSONDeserialize not being a valid string. A variable or parameter of type Text must always be enclosed in double quotes, because it is string. Note this is the way on about every programming language, so no surprise there.
Secondly, like I wrote in my other post in this topic, when you want to include double quotes in a string literal in OutSystems, you use double double quotes. So the correct way to represent this text:
in an OutSystems string literal for assigning to a Text variable or parameter is this:
"[{""Project"":{""Id"":1,""Name"":""First Project""}},{""Project"":{""Id"":2,""Name"":""Second Project""}}]"
This is similar to the way SQL handles its single quote string delimiters, and also similar to escape-quote (\") of C and C++ (and C#, if you don't use the @ string literal).
Ok, now to the errors you got. The first error, "Syntax error caused by unexpected '[' element in expression." is typically thrown when you pass a JSON array, i.e. a collection or list of multiple records, to a single record, instead of a list of records, in OutSystems. If you JSON Deserialize a JSON string starting with "[" into a single record, you get that error. Make sure you have a List of the structure type, and not a variable that's not a list of that structure type.
As for the second error, "Syntax error caused by unexpected ':' element in expression.", I'm pretty sure that's an actual syntax error, caused by you not removing the brackets correctly, and/or just removing the brackets but still leaving multiple records in the JSON string, which is also invalid JSON.
I hope the above explains it and you can fix your code. If not, please add the module (OML file) so I can check what goes wrong and why.