0

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"
  }
]
2
  • 2
    Yes, your JSON is an array. Attempt something like: var array = JArray.Parse(jStr);. Commented Jan 12, 2022 at 9:06
  • see this question stackoverflow.com/questions/40790217/… Commented Jan 12, 2022 at 9:11

2 Answers 2

3

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

right and that works, i tried that. But I would like some way that I take each object in the array and pass them one by one . How do i do that?
there are multiple objects in the array, i just gave one to show the sample
2

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
}

14 Comments

Absolutely not, this is perfectly valid JSON (RFC 8259)
but then that will be with "Data" as key. I am trying to modify my values in the json
my jobject is later passed to another method which takes a Jobject but not an array
@NicolasVoron I edited my answer and add word object: ...a valid json object
@ZZZSharePoint Your question was how to fix it. What object is required to get in the end to pass on, I can't know. This is a completely different question Please provide the resulting JObject structure to use
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.