0
votes

I issue a request to an API, and I'm able to parse the success response result (response.Content.ReadAsStringAsync().Result), but when I get a failure response ("bad request"), the seemingly well-formed JSON result cannot be parsed by JObject.Parse() or JToken.Parse(), they throw.

using System;
using Newtonsoft.Json.Linq;

public class Program
{
    public static void Main()
    {
        var result = "\"{\\\"error\\\":{\\\"detail\\\":\\\"java.lang.NullPointerException\\\",\\\"message\\\":\\\"Error occured while setting field 'caller_id' with value null\\\"},\\\"status\\\":\\\"failure\\\"}\"";

        var jObject = JObject.Parse(result);

        Console.WriteLine(jObject["error"]["message"]);
    }
}

The exception:

Run-time exception (line 10): Error reading JObject from JsonReader. Current JsonReader item is not an object: String. Path '', line 1, position 162.

Stack Trace:

[Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader. Current JsonReader item is not an object: String. Path '', line 1, position 162.] at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings) at Newtonsoft.Json.Linq.JObject.Parse(String json, JsonLoadSettings settings) at Newtonsoft.Json.Linq.JObject.Parse(String json) at Program.Main() :line 10

The exception points to the very end of the string, but I can't figure out what's missing. https://jsonblob.com can parse it after I remove the backslashes, I got rid of the apostrophe's, I "cleaned it up" with string and regex replace, I turned into a char array and build it again, all to no avail.

Help appreciated!

2
Why do you have so many slashes in your string? I ran your code with this string var result = "{\"error\":{\"detail\":\"java.lang.NullPointerException\",\"message\":\"Error occured while setting field 'caller_id' with value null\"},\"status\":\"failure\"}"; successfully.yW0K5o
That's what comes back from the request, I'm guessing C# escapes a bit more.hector-j-rivas

2 Answers

1
votes

The string you provide is well-formed JSON. It's what you get if you serialize some data to a JSON string and then serialize that string to a JSON string. To get the data out of the original JSON string, you therefore have to parse it twice.

The following works for me:

        var jToken = JToken.Parse(result);
        var jObject = JObject.Parse((string)((JValue)jToken).Value);

        Console.WriteLine(jObject["error"]["message"]);
1
votes

When I console write your json string which is:

var result = "\"{\\\"error\\\":{\\\"detail\\\":\\\"java.lang.NullPointerException\\\",\\\"message\\\":\\\"Error occured while setting field 'caller_id' with value null\\\"},\\\"status\\\":\\\"failure\\\"}\"";

it returns:

"{\"error\":{\"detail\":\"java.lang.NullPointerException\",\"message\":\"Error occured while setting field 'caller_id' with value null\"},\"status\":\"failure\"}"

Which is not a correct json string. Your json string has to be like below:

var result = "{\"error\":{\"detail\":\"java.lang.NullPointerException\",\"message\":\"Error occured while setting field 'caller_id' with value null\"},\"status\":\"failure\"}"

So that when you console write this statement you will see that it generates below string which is a correct formed json:

{"error":{"detail":"java.lang.NullPointerException","message":"Error occured while setting field 'caller_id' with value null"},"status":"failure"}