Hi, I’m trying to deserialize a string that contains JSON. It contains multiple entries.
Some context: I’m taking text from the body of an email (there’s plaintext JSON at the end of it) for survey results and outputting it into excel. But I’ve been unable to separate the questions and answers because of how it’s formatted.
For example:
{“question”:“Reference Number”,“answer”:“847564592”},{“question”:“Timestamp”,“answer”:“Mon, 16 Mar 2020 02:10:46 PM”},{“question”:“Name”,“answer”:“Alex Bob Johanson”},{“question”:“Do you enjoy corn soup?”,“answer”:“Yes”},{“question”:“Where are you from?”,“answer”:“Singapore”}
I’ve used Replace(“”“”,“'”) to change the " to '. The problem is that I can’t split the string with the commas because the array will output something like:
{‘question’:‘Reference Number’
it’s incomplete.
Is there any way to work around this? Any help is appreciated, thank you!
I’ve managed to work around separating the values and putting them into an array (where 1 array element is “{‘question’:‘Name’,‘answer’:‘Alex’}”) and I tried putting it into the JSON deserializer and I’m getting an error:
Deserialize JSON: Unable to cast object of type ‘Newtonsoft.Json.Linq.JValue’ to type ‘Newtonsoft.Json.Linq.JObject’.
The final idea is the questions and answers sorted in an excel sheet. But for now I’m trying to see if I can get my message box to show a question or answer when I call for it…
My JSON is in a string actually. I’ll try your steps skipping the text file parts. If not, I guess I’ll have to try to figure out a workaround. Thanks! Will update in an hour or two
I’ll summarise couple of problems I’ve encountered…
I put a JSON into a string (“{‘question’:‘Name’,‘answer’:‘Alex’}”)
Put it into the JSON deserializer and set the output to out_Json (type: JObject, Type Argument is also JObject) But I get the Deserialize JSON: Unable to cast object of type ‘Newtonsoft.Json.Linq.JValue’ to type ‘Newtonsoft.Json.Linq.JObject’. error.
I’ve tried making the JSON deserializer output variable a datatable and changed the type. But I encountered this error instead
Check the Text File, I had Copy pasted the Data from the link you have provided. It worked for me. Can you Check if that is the way you needed it. And if you may, Spot the difference in your workflow and mine
Looks like my problem was editing how the JSON string worked, since I changed the " to ’ and other things with this line
JSONstring.Replace(“[”,“”).Replace(“]”,“”).Replace(“”“”,“'”).Replace(“{”,“”“{”).Replace(“}”,“}”“”).Replace(“”“,”, “”“”+Environment.NewLine)