LINQ to JSON - Query for object or an array <p>I'm trying to get a list of SEDOL's &amp; ADP values. Below is my json text:</p> <pre><code>{ "DataFeed" : { "@FeedName" : "AdminData", "Issuer" : [{ "id" : "1528", "name" : "ZYZ.A a Test Company", "clientCode" : "ZYZ.A", "securities" : { "Security" : { "id" : "1537", "sedol" : "SEDOL111", "coverage" : { "Coverage" : [{ "analyst" : { "@id" : "164", "@clientCode" : "SJ", "@firstName" : "Steve", "@lastName" : "Jobs", "@rank" : "1" } }, { "analyst" : { "@id" : "261", "@clientCode" : "BG", "@firstName" : "Bill", "@lastName" : "Gates", "@rank" : "2" } } ] }, "customFields" : { "customField" : [{ "@name" : "ADP Security Code", "@type" : "Textbox", "values" : { "value" : "ADPSC1111" } }, { "@name" : "Top 10 - Select one or many", "@type" : "Dropdown, multiple choice", "values" : { "value" : ["Large Cap", "Cdn Small Cap", "Income"] } } ] } } } }, { "id" : "1519", "name" : "ZVV Test", "clientCode" : "ZVV=US", "securities" : { "Security" : [{ "id" : "1522", "sedol" : "SEDOL112", "coverage" : { "Coverage" : { "analyst" : { "@id" : "79", "@clientCode" : "MJ", "@firstName" : "Michael", "@lastName" : "Jordan", "@rank" : "1" } } }, "customFields" : { "customField" : [{ "@name" : "ADP Security Code", "@type" : "Textbox", "values" : { "value" : "ADPS1133" } }, { "@name" : "Top 10 - Select one or many", "@type" : "Dropdown, multiple choice", "values" : { "value" : ["Large Cap", "Cdn Small Cap", "Income"] } } ] } }, { "id" : "1542", "sedol" : "SEDOL112", "customFields" : { "customField" : [{ "@name" : "ADP Security Code", "@type" : "Textbox", "values" : { "value" : "ADPS1133" } }, { "@name" : "Top 10 - Select one or many", "@type" : "Dropdown, multiple choice", "values" : { "value" : ["Large Cap", "Cdn Small Cap", "Income"] } } ] } } ] } } ] } } </code></pre> <p>Here's the code that I have so far:</p> <pre><code>var compInfo = feed["DataFeed"]["Issuer"] .Select(p =&gt; new { Id = p["id"], CompName = p["name"], SEDOL = p["securities"]["Security"].OfType&lt;JArray&gt;() ? p["securities"]["Security"][0]["sedol"] : p["securities"]["Security"]["sedol"] ADP = p["securities"]["Security"].OfType&lt;JArray&gt;() ? p["securities"]["Security"][0]["customFields"]["customField"][0]["values"]["value"] : p["securities"]["Security"]["customFields"]["customField"][0]["values"]["value"] }); </code></pre> <p>The error I get is:</p> <blockquote> <p>Accessed JArray values with invalid key value: "sedol". Int32 array index expected</p> </blockquote> <p>I think I'm really close to figuring this out. What should I do to fix the code? If there is an alternative to get the <code>SEDOL</code> and <code>ADP value</code>, please do let me know?</p> <p><strong>[UPDATE1]</strong> I've started working with dynamic ExpandoObject. Here's the code that I've used so far:</p> <pre><code>dynamic obj = JsonConvert.DeserializeObject&lt;ExpandoObject&gt;(json, new ExpandoObjectConverter()); foreach (dynamic element in obj) { Console.WriteLine(element.DataFeed.Issuer[0].id); Console.WriteLine(element.DataFeed.Issuer[0].securities.Security.sedol); Console.ReadLine(); } </code></pre> <p>But I'm now getting the error <code>'ExpandoObject' does not contain a definition for 'DataFeed' and no extension method 'DataFeed' accepting a first argument of type 'ExpandoObject' could be found</code>. <em>NOTE:</em> I understand that this json text is malformed. One instance has an array &amp; the other is an object. I want the code to be agile enough to handle both instances.</p> <p><strong>[UPDATE2]</strong> Thanks to @dbc for helping me with my code so far. I've updated the json text above to closely match my current environment. I'm now able to get the SEDOLs &amp; ADP codes. However, when I'm trying to get the 1st analyst, my code only works on objects and produces nulls for the analysts that are part of an array. Here's my current code:</p> <pre><code>var compInfo = from issuer in feed.SelectTokens("DataFeed.Issuer").SelectMany(i =&gt; i.ObjectsOrSelf()) let security = issuer.SelectTokens("securities.Security").SelectMany(s =&gt; s.ObjectsOrSelf()).FirstOrDefault() where security != null select new { Id = (string)issuer["id"], // Change to (string)issuer["id"] if id is not necessarily numeric. CompName = (string)issuer["name"], SEDOL = (string)security["sedol"], ADP = security["customFields"] .DescendantsAndSelf() .OfType&lt;JObject&gt;() .Where(o =&gt; (string)o["@name"] == "ADP Security Code") .Select(o =&gt; (string)o.SelectToken("values.value")) .FirstOrDefault(), Analyst = security["coverage"] .DescendantsAndSelf() .OfType&lt;JObject&gt;() .Select(jo =&gt; (string)jo.SelectToken("Coverage.analyst.@lastName")) .FirstOrDefault(), }; </code></pre> <p>What do I need to change to always select the 1st analyst?</p>