1

I just want to get this JSON into some kind of object. JArray and JToken are completely confusing to me.

I can create a class so that Newtonsoft knows what to map to but if you will notice the objects have the structure of: { "anAnimal": { foo: 1, bar: 2 }} and I don't know what that mapper object will look like. I'm pretty sure this should just work instantly with zero thought on my part.

var myFavoriteAnimalsJson = @"
[
    {
        ""Dog"": {
            ""cuteness"": ""7.123"",
            ""usefulness"": ""5.2"",
        }
    },
    {
        ""Cat"": {
            ""cuteness"": ""8.3"",
            ""usefulness"": ""0"",
        }
    }
]";

var jArray = new JArray(myFavoriteAnimalsJson);
// grab the dog object. or the cat object. HOW CUTE IS THE DOG? 
3
  • 1
    Is the Dog object always going to be the first in the array, or do you need to determine which one it is? Commented May 18, 2022 at 2:45
  • 2
    Do you control the schema of this JSON? If so, you might want to simplify it. Your array has a list of objects, which has a property of Dog or Cat, which those properties themselves are objects. Might be easier to move everything "up one level". Commented May 18, 2022 at 2:48
  • no, sometimes it will be dog. sometimes it will be killer whale. nope, i don't control it. I could grab this with node.js easily and parse it into something a little easier for c# but that's too much. once this works it will be way better. Commented May 18, 2022 at 2:52

3 Answers 3

4

With .SelectToken() to construct the JSON path query logic.

The below sample to query the first item of animals to get the object of "Dog" token and its value.

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

JArray animals = JArray.Parse(myFavoriteAnimalsJson);
        
var dog = animals[0].SelectToken("Dog");

Console.WriteLine(dog);
Console.WriteLine(dog["cuteness"]);

Sample Program

Output

{ "cuteness": "7.123", "usefulness": "5.2" }

7.123

Sign up to request clarification or add additional context in comments.

3 Comments

Just a note, this assumes that Dog is always the first element. You can pass a more complex JSONPath to SelectToken when it's not in the first element. animals.SelectToken("$[*].Dog");
Hi @gunr2171, good hints and explanation for the scenario! Thanks for the guidance. =)
Oh and as for the getting the identifier of each object (Cat, Dog) I found this code which works. javaer101.com/en/article/8638748.html var jArray = JArray.Parse(myFavoriteAnimalsJson); var animals = jArray.Children<JObject>(); foreach(var animal in animals) { foreach(JProperty prop in animal.Properties()) { Console.WriteLine(prop.Name); } } How is the the standard C# JSON library. Pure crap UI IMO.
2

You can deserialize it to a List<Dictionary<string, AnimalData>>

class AnimalData
{
    public decimal cuteness;
    public decimal usefulness;
}
var myFavoriteAnimalsJson = @"
[
    {
        ""Dog"": {
            ""cuteness"": ""7.123"",
            ""usefulness"": ""5.2"",
        }
    },
    {
        ""Cat"": {
            ""cuteness"": ""8.3"",
            ""usefulness"": ""0"",
        }
    }
]";

var results = JsonConvert.DeserializeObject<List<Dictionary<string, AnimalData>>>(myFavoriteAnimalsJson);

Now each list item contains a dictionary with a single key of Dog Cat...

1 Comment

Nice! This is actually what I was trying to do. I had the Dictionary<string, animal> but not the List.
0

If you start from a serialized JSON, i.e. a string, you have to parse it:

var jArray = JArray.Parse(myFavoriteAnimalsJson);

2 Comments

okay that works but then it gives me a JToken. i hate to ask but ...how does one read information out of a JToken? For example, how would I know if I have the Dog or Cat JToken?
There is some docs about this library here: newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm The idea behind it is to shape the JSON structure into a node tree, so that it can be transversed and manipulated easily. In the past, I used a lot this way to manage JSON, although it's rather expensive in terms of memory occupation.

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.