0

I am facing issue with deserializing json which contains value with special character (i.e. quote).

I am loading json from file (its adaptive card).

{
    "type": "AdaptiveCard",
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.3",
    "body": [
        {
            "type": "ColumnSet",
            "columns": [
                {
                    "type": "Column",
                    "width": "stretch",
                    "items": [
                        {
                            "type": "TextBlock",
                            "text": "<TitleValue>",
                            "wrap": true,
                            "size": "Large"
                        }
                    ]
                },
                {
                    "type": "Column",
                    "width": "auto",
                    "items": [
                        {
                            "type": "Image",
                            "url": "https://adaptivecards.io/content/cats/1.png",
                            "size": "Small"
                        }
                    ]
                }
            ]
        },
        {
            "type": "TextBlock",
            "text": "<BodyValue>",
            "wrap": true,
            "size": "Medium"
        }
    ],
    "minHeight": "206px"
}

Then I am replacing two placeholders with strings i got from the chatbot and generate adaptive card attachment.

public static Attachment CreateNoteCardAttachment(string noteTitle, string noteBody)
    {
        // combine path for cross platform support
        var paths = new[] { ".", "Resources", "NoteAdaptiveCard.txt" };
        var noteCardJson = File.ReadAllText(Path.Combine(paths));
        noteCardJson = noteCardJson.Replace("<TitleValue>", noteTitle);
        noteCardJson = noteCardJson.Replace("<BodyValue>", noteBody);

        var noteCardAttachment = new Attachment()
        {
            ContentType = "application/vnd.microsoft.card.adaptive",
            Content = JsonConvert.DeserializeObject(noteCardJson),
        };

        return noteCardAttachment;
    }

The issue occurs, when the string contains double quote. Then JsonConvert.DeserializeObject returns exception: After parsing a value an unexpected character was encountered...

I am not able to figure out how to parse/deserialise the input strings I replace in order to allow JsonConvert to successfully deserialize the entire adaptive card.

3
  • 3
    Simple answer: stop modifying your JSON using string.Replace! Commented Aug 11, 2021 at 14:10
  • 1
    Not an exact duplicate but you are not sanitizing your input. This should help: stackoverflow.com/questions/10756232/… Commented Aug 11, 2021 at 14:16
  • 1
    What Llama means is that you first should deserialize your JSON to an object (using something like json2csharp.com to generate to C# class or do it manually) and then change the value you want. String operations are flaky at best
    – MindSwipe
    Commented Aug 11, 2021 at 15:15

0

Browse other questions tagged or ask your own question.