Solved Error : Reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path


We encountered an error while trying to read a JObject from a JsonReader, error message indicates that the current JsonReader item is not an object, rather, it is a StartArray this error typically occurs when we attempt to read an array as an object.

For example, consider the following JSON data:

    
[
  {
    "name": "Johny",
    "age": 30
  },
  {
    "name": "Janes",
    "age": 25
  }
]
    
  

In this JSON data, we have an array of objects, each representing a person with properties like name and age. If we mistakenly try to read this data as a single object instead of an array, we will encounter the mentioned error.

To resolve this issue, we need to ensure that our code handles arrays correctly when reading JSON data. We should first check whether the current item being read is an array or an object, and then process it accordingly.

After encountering the issue where the returned JSON was in the form of an Array/List rather than an Object, We using JArray.Parse, which effectively resolved the problem. 

    
jArray = JArray.Parse(jsoncontent);
    
  

JArray.Parse is designed to handle JSON arrays specifically. By utilizing this method, we were able to parse the JSON content accurately and proceed with our operations.


While working on our Xamarin application, we encountered an issue when trying to parse JSON data using the following code:

    
var result= JsonConvert.DeserializeObject<LocationData>(respJsonArray.ToString(), jsonSettings);

The error message "Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path" suggests that the JSON data we're trying to parse is not in the expected format.

Let's consider an example JSON data:

    
[
  {
    "name": "New York",
    "latitude": 40.7128,
    "longitude": -74.0060
  },
  {
    "name": "Los Angeles",
    "latitude": 34.0522,
    "longitude": -118.2437
  }
]
    
  

In this example, we have an array of objects representing locations with properties like name, latitude, and longitude. However, the code is expecting a single object (JObject) but encounters a StartArray instead.

var result = JsonConvert.DeserializeObject<List<ShopLocation>>(respJsonArray.ToString(), jsonSettings
);
This modification ensures that the JSON array is deserialized into a list of ShopLocation objects, accommodating the array structure of the JSON data.

To resolve this issue, we need to change our code to handle JSON arrays correctly. We can either deserialize the JSON array directly into a collection type like a list, or we can modify our code to expect an array instead of a single object.

When we encountered a similar issue with our Xamarin app, we found a solution that avoided re-serializing and parsing the nested JSON object. Instead, we used the following approach:

    
var jsonObjects = jsonArray.OfType<JObject>().ToList();
    
  

Extract the items in the JSON array as objects directly, avoiding the need for additional serialization and parsing.

Additionally, we found that the reason JObject.Parse(json) didn't work for us was due to our JSON having square brackets "[" and "]" at the beginning and end. To address this, we removed those characters from the JSON string:

    
jsonStr = jsonStr.TrimStart(new char[] { '[' }).TrimEnd(new char[] { ']' });
    
  

After this adjustment, we were able to successfully use JObject.Parse(jsonResult) without encountering any errors.

Overall, this solution provided a more efficient way to handle JSON arrays and resolved the parsing issue we faced in our Xamarin application.

The error message "Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path" typically occurs when using JArray.Parse to parse JSON data that starts with an array ([) instead of an object ({).

In simpler terms, JArray.Parse expects a JSON array as input, but it's encountering a JSON object or something else at the beginning of the JSON data.

To fix this issue, ensure that you are passing a valid JSON array to JArray.Parse. If your JSON data starts with [, it's an array, and you should use JArray.Parse. However, if your JSON data starts with {, it's an object, and you should use JObject.Parse instead.

See Below example:

    
JArray jsonArray = JArray.Parse("[1, 2, 3]"); // Valid JSON array
JObject jsonObject = JObject.Parse("{\"Name\": \"Alok\"}"); // Valid JSON object
    
  

Ensure that the JSON data matches the expected format for parsing with JArray.Parse, and you should be able to resolve this error.

In my application, I encounter an error when passing a string in my code with the line var json = JObject.Parse(jsonstr);. The error message reads:

    
System.Private.CoreLib: Exception while executing function: Function1. Newtonsoft.Json: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray.
    


[
    {
        "country": "India",
        "city": "New Market, Kolkata",
        "area": "West Bengal",
        "Pincode": "700087",
        "street": "",
        "house_no": "",
        "longitude": 88.3639,
        "latitude": 22.5555,
        "address": "New Market, Kolkata 700087, West Bengal, India"
    },
    {
        "country": "India",
        "city": "MG Road, Pune",
        "area": "Maharashtra",
        "Pincode": "411001",
        "street": "",
        "house_no": "",
        "longitude": 73.8567,
        "latitude": 18.5204,
        "address": "MG Road, Pune 411001, Maharashtra, India"
    },
    {
        "country": "India",
        "city": "Connaught Place, New Delhi",
        "area": "Delhi",
        "Pincode": "110001",
        "street": "",
        "house_no": "",
        "longitude": 77.2206,
        "latitude": 28.6315,
        "address": "Connaught Place, New Delhi 110001, Delhi, India"
    },
    {
        "country": "India",
        "city": "Majestic, Bangalore",
        "area": "Karnataka",
        "Pincode": "560009",
        "street": "",
        "house_no": "",
        "longitude": 77.5763,
        "latitude": 12.9750,
        "address": "Majestic, Bangalore 560009, Karnataka, India"
    }
]

When encountering a situation where we need to wrap a string to make a valid JSON object, we can employ the following approach:

    
var json = JObject.Parse("{ \"JsonData\":" + jsonString + "}");
    
  

With this code, the resulting object will contain a property named "JsonData" containing the data from the original JSON string.

Another approach we can consider is iterating through each object in a JSON array:

    
foreach (JObject obj in JArray.Parse(jsonString))
{
  // Process each object here
}
    
  

The first approach allows us to easily wrap a string to create a valid JSON object, which can be particularly helpful when integrating with APIs or handling dynamic data.

The second approach is good when dealing with JSON arrays, as it enables us to iterate through each object within the array and perform necessary operations.