Accepted answer

You don't show how you initialize fakeGuidForExecutiveSummary. Assuming you do it in the following way:

        string fakeResponseFromExecutiveSummaryProxy = "{ \"DocumentID\": \"fakeGuid1\",\"documentNotes\": \"TestNotes1\"}";
        var fakeResponse = JToken.Parse(fakeResponseFromExecutiveSummaryProxy);
        var fakeGuidForExecutiveSummary = fakeResponse["DocumentID"];

Then the problem is that fakeGuidForExecutiveSummary is a JValue, not a JToken or JArray. Your code will throw the exception you see if you try to access a (nonexistent) child value by index.

Instead you need to do the following:

        string response = @"[{ ""DocumentID"": ""fakeGuid1"",""documentNotes"": ""TestNotes1""}]";
        JArray jsonResponse = JArray.Parse(response);
        JToken token = jsonResponse[0];

        //Value of token from Debugger - { "DocumentID": fakeGuid1","documentNotes": "TestNotes1"}
        Assert.AreEqual(fakeGuidForExecutiveSummary, token["DocumentID"])


Given your updated code, the problem is that your sample JSON response has too many levels of string escaping: \\\"DocumentID\\\". You probably copied escaped strings shown in Visual Studio into the source code, then escaped them some again.

Change it to

        string response = "[\r\n  { \"DocumentID\": \"fakeGuid1\",\"documentNotes\": \"TestNotes1\"}\r\n]";

Related Articles