ZZZSharePoint
ZZZSharePoint

Reputation: 1351

Newtonsoft.Json: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray

Hi I am passing the below string in my code in line var json = JObject.Parse(jStr); and I get the error as

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

Can someone please explain me how should I fix this. Is it because it is an array kind? At the end my passed string will be an array. how can I fix this?. here is the sample string(one object from the array) which I am passing in above line

[
  {
    "_id": "_AUQ9",
    "im:SiceErrors": {
      "errors": [
        {
          "_id": "O9UQ9_0",
          "ErrorMessage": "Uype",          
          "Parameter": "r:type",
          "ParameterValue": "[\n  \"~:bbl:P924\",\n  \"~:corer:ag\"\n]",
          "RefObjectParameter": "type_key",
          "Project": "NPOS+1"
        }
      ]
    },
    "cullad-tag:Cject": "P|GT",
    "bbl:P1014": "PIGT",
    "cullad-tag:MoomosBaseObjectDescription": "Gate Valve",
    "bbl:P3935": "Gve",
    "cullad-tag:CreateDate": "15.10.2021",
    "cullad-tag:MoomosDescription": "P2",
    "cullad-tag:MoomosUID": "A49",
    "bbl:011772": "UQ9",
    "cullad-tag:Description": "P2",
    "cullad-tag:Discipline": "U",
    "bbl:P101001445": "bbl:P163",
    "cullad-tag:FLabel": "FFeed",
    "bbl:P1094": "pd:P1299",   
    "bbl:P10791": "V",
    "cullad-tag:FutureTag": "No",
    "cullad-tag:IndexType": "MV",
    "bbl:P138": "bbl:P1563",
    "cullad-tag:IsTopTag": "No",
    "bbl:P7": "No",
    "cullad-tag:MountedOn": "F-18UD800",
    "bbl:P4024": "F-18800",   
    "bbl:P1834": "pd:P1094",    
    "bbl:P1803": "8120",
    "cullad-tag:SubProjectCategory": "Feed",
    "cullad-tag:SubProjectName": "SUB",
    "cullad-tag:System": "18",
    "bbl:P01660": "bbl:P15326",
    "cullad-tag:TagNumber": "F-120",
    "bbl:P10022": "F-20",   
    "cdf:type": [
      "bbl:P10924",
      "bbl:P101907",
      "bbl:P101003632",
      "bbl:P13932",
      "bbl:P21",
      "iso15926:InanimatePject",
      "iso15926:Object",
      "Moomos:PopoFeedTag"
    ],
    "primarycdf:type": "bbl:P924"
  }
]

Upvotes: 0

Views: 10887

Answers (2)

SeeSharp
SeeSharp

Reputation: 1750

You have to wrap your string to make a valid json object:

var json = JObject.Parse("{ \"Data\":" + jStr + "}");

The resulting object will contain property Data with data from json

edited by comments: OR try this approach:

foreach(JObject obj in JArray.Parse(jStr))
{
    // todo: pass an object to a method that requires an JObject to process
}

Upvotes: 2

Nicolas Voron
Nicolas Voron

Reputation: 3006

Your example is a single object inside an array.

@SeeSharp propose to wrap that array.

The opposite way is, if that's always the case, that you can just parse the object inside that array (removing first and last character):

var json = JObject.Parse(jStr.Remove(str.Length - 1).Remove(0, 1));

Otherwise, I guess you just adapt your code to handle an array, and not an object (which looks the better way to do so)

EDIT :

Since there is multiple object inside your JArray, just iterate over it :

foreach (JObject item in JArray.Parse(jStr)) 
{
  DoStuffOnJObject(item) // call your method
}

Upvotes: 3

Related Questions