How to check In C# JArray have specific key pair












0















I am working on JArray in .NET CORE and I am getting random structure of this one specific key, hence getting different error. I need to know if JArray has specific child Array and if Child JArray have specific key pair (NOT VALUE) i.e. value{ "Id":""} one of error is following;



 Accessed JArray values with invalid key value: "id". Int32 array index expected.
at Newtonsoft.Json.Linq.JArray.get_Item(Object key) at


the standard structure I am expecting is as following;



{[value, [
{
"id": "7ef82869-e235-69a2-f81e-3a9664e89bc4",
"value": ""
}
]]}


sometime I get as, meaning throw null error where I am trying to map Id.



 {[value, [
{
"value": ""
}
]]}


and some Time I don't get this property at all



I am trying following check to cover all scenario but Its not really working.



code



 if (answerItems.value != null && answerItems.value.HasValues && answerItems.value["id"]!=null)
{









share|improve this question























  • The "standard structure" you've shown is not valid JSON, so it's difficult for us to give you a specific answer. But it looks like you're trying to access "id" as a property on the array itself, rather than the property of a specific item inside. You could use LINQ to query the objects in the array, or you could use the SelectToken method to get an item that matches criteria you define in a string (e.g. answerItems.value.SelectToken("[?(@.id == '')]")).

    – StriplingWarrior
    Nov 22 '18 at 0:21
















0















I am working on JArray in .NET CORE and I am getting random structure of this one specific key, hence getting different error. I need to know if JArray has specific child Array and if Child JArray have specific key pair (NOT VALUE) i.e. value{ "Id":""} one of error is following;



 Accessed JArray values with invalid key value: "id". Int32 array index expected.
at Newtonsoft.Json.Linq.JArray.get_Item(Object key) at


the standard structure I am expecting is as following;



{[value, [
{
"id": "7ef82869-e235-69a2-f81e-3a9664e89bc4",
"value": ""
}
]]}


sometime I get as, meaning throw null error where I am trying to map Id.



 {[value, [
{
"value": ""
}
]]}


and some Time I don't get this property at all



I am trying following check to cover all scenario but Its not really working.



code



 if (answerItems.value != null && answerItems.value.HasValues && answerItems.value["id"]!=null)
{









share|improve this question























  • The "standard structure" you've shown is not valid JSON, so it's difficult for us to give you a specific answer. But it looks like you're trying to access "id" as a property on the array itself, rather than the property of a specific item inside. You could use LINQ to query the objects in the array, or you could use the SelectToken method to get an item that matches criteria you define in a string (e.g. answerItems.value.SelectToken("[?(@.id == '')]")).

    – StriplingWarrior
    Nov 22 '18 at 0:21














0












0








0


1






I am working on JArray in .NET CORE and I am getting random structure of this one specific key, hence getting different error. I need to know if JArray has specific child Array and if Child JArray have specific key pair (NOT VALUE) i.e. value{ "Id":""} one of error is following;



 Accessed JArray values with invalid key value: "id". Int32 array index expected.
at Newtonsoft.Json.Linq.JArray.get_Item(Object key) at


the standard structure I am expecting is as following;



{[value, [
{
"id": "7ef82869-e235-69a2-f81e-3a9664e89bc4",
"value": ""
}
]]}


sometime I get as, meaning throw null error where I am trying to map Id.



 {[value, [
{
"value": ""
}
]]}


and some Time I don't get this property at all



I am trying following check to cover all scenario but Its not really working.



code



 if (answerItems.value != null && answerItems.value.HasValues && answerItems.value["id"]!=null)
{









share|improve this question














I am working on JArray in .NET CORE and I am getting random structure of this one specific key, hence getting different error. I need to know if JArray has specific child Array and if Child JArray have specific key pair (NOT VALUE) i.e. value{ "Id":""} one of error is following;



 Accessed JArray values with invalid key value: "id". Int32 array index expected.
at Newtonsoft.Json.Linq.JArray.get_Item(Object key) at


the standard structure I am expecting is as following;



{[value, [
{
"id": "7ef82869-e235-69a2-f81e-3a9664e89bc4",
"value": ""
}
]]}


sometime I get as, meaning throw null error where I am trying to map Id.



 {[value, [
{
"value": ""
}
]]}


and some Time I don't get this property at all



I am trying following check to cover all scenario but Its not really working.



code



 if (answerItems.value != null && answerItems.value.HasValues && answerItems.value["id"]!=null)
{






c# json .net-core json.net






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 0:02









khurramkhurram

1,9921058120




1,9921058120













  • The "standard structure" you've shown is not valid JSON, so it's difficult for us to give you a specific answer. But it looks like you're trying to access "id" as a property on the array itself, rather than the property of a specific item inside. You could use LINQ to query the objects in the array, or you could use the SelectToken method to get an item that matches criteria you define in a string (e.g. answerItems.value.SelectToken("[?(@.id == '')]")).

    – StriplingWarrior
    Nov 22 '18 at 0:21



















  • The "standard structure" you've shown is not valid JSON, so it's difficult for us to give you a specific answer. But it looks like you're trying to access "id" as a property on the array itself, rather than the property of a specific item inside. You could use LINQ to query the objects in the array, or you could use the SelectToken method to get an item that matches criteria you define in a string (e.g. answerItems.value.SelectToken("[?(@.id == '')]")).

    – StriplingWarrior
    Nov 22 '18 at 0:21

















The "standard structure" you've shown is not valid JSON, so it's difficult for us to give you a specific answer. But it looks like you're trying to access "id" as a property on the array itself, rather than the property of a specific item inside. You could use LINQ to query the objects in the array, or you could use the SelectToken method to get an item that matches criteria you define in a string (e.g. answerItems.value.SelectToken("[?(@.id == '')]")).

– StriplingWarrior
Nov 22 '18 at 0:21





The "standard structure" you've shown is not valid JSON, so it's difficult for us to give you a specific answer. But it looks like you're trying to access "id" as a property on the array itself, rather than the property of a specific item inside. You could use LINQ to query the objects in the array, or you could use the SelectToken method to get an item that matches criteria you define in a string (e.g. answerItems.value.SelectToken("[?(@.id == '')]")).

– StriplingWarrior
Nov 22 '18 at 0:21












1 Answer
1






active

oldest

votes


















2














I received some constructive criticism on the brevity of my answer so I figured I would elaborate to help you through your issue.



First, let's take a look at your JSON. In short, it isn't valid. In fact, it isn't even close to valid JSON. I can only assume you meant something like this:



{
"values" : [{ "id" : "7ef82869-e235-69a2-f81e-3a9664e89bc4", "value": "" }]
}


I would suggest that anytime you are trying to parse data like this and you run into issues you start trouble shooting by validating the data itself. I like to use JSONLint for this.



Next, it is difficult from your example code to tell exactly what you are trying to do. I can only guess that you are attempting to use the dynamic object method of parsing and working with the data. A downside to this method is it is difficult to validate your data before you work with it.



Instead, I would use the Newtonsoft.Json.Linq.JObject.Parse method. This gives you some tools for working with and validating the information. Below I have included a very simple example of how this would be done.



using System;
using Newtonsoft.Json.Linq;

public class Program
{
public static void Main()
{
string json = "{ "values": [{ "id": "7ef82869-e235-69a2-f81e-3a9664e89bc4", "value": "" }] }";
JObject obj = JObject.Parse(json);

// Check to see if we got our value array
if (obj.ContainsKey("values")) {
JArray values = (JArray)obj["values"];

// Do we have any values in our array?
if (values.Count > 0) {
JObject firstItem = (JObject)values[0];

// We check to see if we have an ID parameter
if (firstItem.ContainsKey("id")) {
Console.WriteLine(firstItem["id"]);
}
}
}
}
}


As I mentioned in my original post, I would strongly recommend reviewing the Newtonsoft.Json documentation.






share|improve this answer





















  • 2





    This should probably be a comment, not an answer.

    – StriplingWarrior
    Nov 22 '18 at 0:16






  • 2





    Good catch. I went ahead and elaborated for a better answer. Thanks for the critique!

    – Shawn Lehner
    Nov 22 '18 at 0:58











  • ,many thanks shawn, I try what I learn from you. Json data structure done by someone and i cannot change structure so I have to play with what I have

    – khurram
    Nov 22 '18 at 6:39






  • 1





    If you can’t change the JSON then you won’t be able to use a JSON library to parse it. It isn’t JSON. You would be better off parsing it yourself with a custom parser.

    – Shawn Lehner
    Nov 22 '18 at 16:06











  • This is much improved. Great job.

    – StriplingWarrior
    Nov 24 '18 at 21:44











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422135%2fhow-to-check-in-c-sharp-jarray-have-specific-key-pair%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I received some constructive criticism on the brevity of my answer so I figured I would elaborate to help you through your issue.



First, let's take a look at your JSON. In short, it isn't valid. In fact, it isn't even close to valid JSON. I can only assume you meant something like this:



{
"values" : [{ "id" : "7ef82869-e235-69a2-f81e-3a9664e89bc4", "value": "" }]
}


I would suggest that anytime you are trying to parse data like this and you run into issues you start trouble shooting by validating the data itself. I like to use JSONLint for this.



Next, it is difficult from your example code to tell exactly what you are trying to do. I can only guess that you are attempting to use the dynamic object method of parsing and working with the data. A downside to this method is it is difficult to validate your data before you work with it.



Instead, I would use the Newtonsoft.Json.Linq.JObject.Parse method. This gives you some tools for working with and validating the information. Below I have included a very simple example of how this would be done.



using System;
using Newtonsoft.Json.Linq;

public class Program
{
public static void Main()
{
string json = "{ "values": [{ "id": "7ef82869-e235-69a2-f81e-3a9664e89bc4", "value": "" }] }";
JObject obj = JObject.Parse(json);

// Check to see if we got our value array
if (obj.ContainsKey("values")) {
JArray values = (JArray)obj["values"];

// Do we have any values in our array?
if (values.Count > 0) {
JObject firstItem = (JObject)values[0];

// We check to see if we have an ID parameter
if (firstItem.ContainsKey("id")) {
Console.WriteLine(firstItem["id"]);
}
}
}
}
}


As I mentioned in my original post, I would strongly recommend reviewing the Newtonsoft.Json documentation.






share|improve this answer





















  • 2





    This should probably be a comment, not an answer.

    – StriplingWarrior
    Nov 22 '18 at 0:16






  • 2





    Good catch. I went ahead and elaborated for a better answer. Thanks for the critique!

    – Shawn Lehner
    Nov 22 '18 at 0:58











  • ,many thanks shawn, I try what I learn from you. Json data structure done by someone and i cannot change structure so I have to play with what I have

    – khurram
    Nov 22 '18 at 6:39






  • 1





    If you can’t change the JSON then you won’t be able to use a JSON library to parse it. It isn’t JSON. You would be better off parsing it yourself with a custom parser.

    – Shawn Lehner
    Nov 22 '18 at 16:06











  • This is much improved. Great job.

    – StriplingWarrior
    Nov 24 '18 at 21:44
















2














I received some constructive criticism on the brevity of my answer so I figured I would elaborate to help you through your issue.



First, let's take a look at your JSON. In short, it isn't valid. In fact, it isn't even close to valid JSON. I can only assume you meant something like this:



{
"values" : [{ "id" : "7ef82869-e235-69a2-f81e-3a9664e89bc4", "value": "" }]
}


I would suggest that anytime you are trying to parse data like this and you run into issues you start trouble shooting by validating the data itself. I like to use JSONLint for this.



Next, it is difficult from your example code to tell exactly what you are trying to do. I can only guess that you are attempting to use the dynamic object method of parsing and working with the data. A downside to this method is it is difficult to validate your data before you work with it.



Instead, I would use the Newtonsoft.Json.Linq.JObject.Parse method. This gives you some tools for working with and validating the information. Below I have included a very simple example of how this would be done.



using System;
using Newtonsoft.Json.Linq;

public class Program
{
public static void Main()
{
string json = "{ "values": [{ "id": "7ef82869-e235-69a2-f81e-3a9664e89bc4", "value": "" }] }";
JObject obj = JObject.Parse(json);

// Check to see if we got our value array
if (obj.ContainsKey("values")) {
JArray values = (JArray)obj["values"];

// Do we have any values in our array?
if (values.Count > 0) {
JObject firstItem = (JObject)values[0];

// We check to see if we have an ID parameter
if (firstItem.ContainsKey("id")) {
Console.WriteLine(firstItem["id"]);
}
}
}
}
}


As I mentioned in my original post, I would strongly recommend reviewing the Newtonsoft.Json documentation.






share|improve this answer





















  • 2





    This should probably be a comment, not an answer.

    – StriplingWarrior
    Nov 22 '18 at 0:16






  • 2





    Good catch. I went ahead and elaborated for a better answer. Thanks for the critique!

    – Shawn Lehner
    Nov 22 '18 at 0:58











  • ,many thanks shawn, I try what I learn from you. Json data structure done by someone and i cannot change structure so I have to play with what I have

    – khurram
    Nov 22 '18 at 6:39






  • 1





    If you can’t change the JSON then you won’t be able to use a JSON library to parse it. It isn’t JSON. You would be better off parsing it yourself with a custom parser.

    – Shawn Lehner
    Nov 22 '18 at 16:06











  • This is much improved. Great job.

    – StriplingWarrior
    Nov 24 '18 at 21:44














2












2








2







I received some constructive criticism on the brevity of my answer so I figured I would elaborate to help you through your issue.



First, let's take a look at your JSON. In short, it isn't valid. In fact, it isn't even close to valid JSON. I can only assume you meant something like this:



{
"values" : [{ "id" : "7ef82869-e235-69a2-f81e-3a9664e89bc4", "value": "" }]
}


I would suggest that anytime you are trying to parse data like this and you run into issues you start trouble shooting by validating the data itself. I like to use JSONLint for this.



Next, it is difficult from your example code to tell exactly what you are trying to do. I can only guess that you are attempting to use the dynamic object method of parsing and working with the data. A downside to this method is it is difficult to validate your data before you work with it.



Instead, I would use the Newtonsoft.Json.Linq.JObject.Parse method. This gives you some tools for working with and validating the information. Below I have included a very simple example of how this would be done.



using System;
using Newtonsoft.Json.Linq;

public class Program
{
public static void Main()
{
string json = "{ "values": [{ "id": "7ef82869-e235-69a2-f81e-3a9664e89bc4", "value": "" }] }";
JObject obj = JObject.Parse(json);

// Check to see if we got our value array
if (obj.ContainsKey("values")) {
JArray values = (JArray)obj["values"];

// Do we have any values in our array?
if (values.Count > 0) {
JObject firstItem = (JObject)values[0];

// We check to see if we have an ID parameter
if (firstItem.ContainsKey("id")) {
Console.WriteLine(firstItem["id"]);
}
}
}
}
}


As I mentioned in my original post, I would strongly recommend reviewing the Newtonsoft.Json documentation.






share|improve this answer















I received some constructive criticism on the brevity of my answer so I figured I would elaborate to help you through your issue.



First, let's take a look at your JSON. In short, it isn't valid. In fact, it isn't even close to valid JSON. I can only assume you meant something like this:



{
"values" : [{ "id" : "7ef82869-e235-69a2-f81e-3a9664e89bc4", "value": "" }]
}


I would suggest that anytime you are trying to parse data like this and you run into issues you start trouble shooting by validating the data itself. I like to use JSONLint for this.



Next, it is difficult from your example code to tell exactly what you are trying to do. I can only guess that you are attempting to use the dynamic object method of parsing and working with the data. A downside to this method is it is difficult to validate your data before you work with it.



Instead, I would use the Newtonsoft.Json.Linq.JObject.Parse method. This gives you some tools for working with and validating the information. Below I have included a very simple example of how this would be done.



using System;
using Newtonsoft.Json.Linq;

public class Program
{
public static void Main()
{
string json = "{ "values": [{ "id": "7ef82869-e235-69a2-f81e-3a9664e89bc4", "value": "" }] }";
JObject obj = JObject.Parse(json);

// Check to see if we got our value array
if (obj.ContainsKey("values")) {
JArray values = (JArray)obj["values"];

// Do we have any values in our array?
if (values.Count > 0) {
JObject firstItem = (JObject)values[0];

// We check to see if we have an ID parameter
if (firstItem.ContainsKey("id")) {
Console.WriteLine(firstItem["id"]);
}
}
}
}
}


As I mentioned in my original post, I would strongly recommend reviewing the Newtonsoft.Json documentation.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 0:41

























answered Nov 22 '18 at 0:15









Shawn LehnerShawn Lehner

1,260712




1,260712








  • 2





    This should probably be a comment, not an answer.

    – StriplingWarrior
    Nov 22 '18 at 0:16






  • 2





    Good catch. I went ahead and elaborated for a better answer. Thanks for the critique!

    – Shawn Lehner
    Nov 22 '18 at 0:58











  • ,many thanks shawn, I try what I learn from you. Json data structure done by someone and i cannot change structure so I have to play with what I have

    – khurram
    Nov 22 '18 at 6:39






  • 1





    If you can’t change the JSON then you won’t be able to use a JSON library to parse it. It isn’t JSON. You would be better off parsing it yourself with a custom parser.

    – Shawn Lehner
    Nov 22 '18 at 16:06











  • This is much improved. Great job.

    – StriplingWarrior
    Nov 24 '18 at 21:44














  • 2





    This should probably be a comment, not an answer.

    – StriplingWarrior
    Nov 22 '18 at 0:16






  • 2





    Good catch. I went ahead and elaborated for a better answer. Thanks for the critique!

    – Shawn Lehner
    Nov 22 '18 at 0:58











  • ,many thanks shawn, I try what I learn from you. Json data structure done by someone and i cannot change structure so I have to play with what I have

    – khurram
    Nov 22 '18 at 6:39






  • 1





    If you can’t change the JSON then you won’t be able to use a JSON library to parse it. It isn’t JSON. You would be better off parsing it yourself with a custom parser.

    – Shawn Lehner
    Nov 22 '18 at 16:06











  • This is much improved. Great job.

    – StriplingWarrior
    Nov 24 '18 at 21:44








2




2





This should probably be a comment, not an answer.

– StriplingWarrior
Nov 22 '18 at 0:16





This should probably be a comment, not an answer.

– StriplingWarrior
Nov 22 '18 at 0:16




2




2





Good catch. I went ahead and elaborated for a better answer. Thanks for the critique!

– Shawn Lehner
Nov 22 '18 at 0:58





Good catch. I went ahead and elaborated for a better answer. Thanks for the critique!

– Shawn Lehner
Nov 22 '18 at 0:58













,many thanks shawn, I try what I learn from you. Json data structure done by someone and i cannot change structure so I have to play with what I have

– khurram
Nov 22 '18 at 6:39





,many thanks shawn, I try what I learn from you. Json data structure done by someone and i cannot change structure so I have to play with what I have

– khurram
Nov 22 '18 at 6:39




1




1





If you can’t change the JSON then you won’t be able to use a JSON library to parse it. It isn’t JSON. You would be better off parsing it yourself with a custom parser.

– Shawn Lehner
Nov 22 '18 at 16:06





If you can’t change the JSON then you won’t be able to use a JSON library to parse it. It isn’t JSON. You would be better off parsing it yourself with a custom parser.

– Shawn Lehner
Nov 22 '18 at 16:06













This is much improved. Great job.

– StriplingWarrior
Nov 24 '18 at 21:44





This is much improved. Great job.

– StriplingWarrior
Nov 24 '18 at 21:44


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422135%2fhow-to-check-in-c-sharp-jarray-have-specific-key-pair%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'