0

I have this JSON:

{   
   "fuel_distance": 461.6,
   "fuel_distance_um": "MI",
   "id": "62157",
   "stops": [
       {            
           "id": "zz1dnvtir8j050oW100204",            
           "latitude": 34.0214
       }
    ]
}

and I would like to get the values in the picture.

Visualization:

I'm working on a C# project. I would like to get those using LINQ.

Thank you very much in advance guys!

0

2 Answers 2

1

Demo on dotnet fiddle

You can use Newtonsoft to DeserializeObject then get values as you wish like below.

public class Program
{
    public static void Main()
    {
        var json = "{\"fuel_distance\": 461.6, \"fuel_distance_um\": \"MI\", \"id\": \"62157\", \"stops\": [{ \"id\": \"zz1dnvtir8j050oW100204\", \"latitude\": 34.0214 }]}";
        var deserializedObject = JsonConvert.DeserializeObject<MyModel>(json);
        Console.WriteLine("Id: " + deserializedObject.id);

        var stopIds = deserializedObject.stops.Select(p => p.id);
        foreach(var id in stopIds)
          Console.WriteLine("Id child: "+ id);
        }
}

public class MyModel 
{
    public decimal fuel_distance {get; set;}
    public string fuel_distance_um {get; set;}
    public string id {get; set;}
    public MyChildModel[] stops {get; set;}
}

public class MyChildModel 
{
    public string id {get; set;}
    public decimal latitude {get; set;}
}

Output

Id: 62157
Id child: zz1dnvtir8j050oW100204

Updated

You can also get the result like below

    var result = new
    { 
        ID = deserializedObject.id, 
        stopId = deserializedObject.stops[0].id
    };
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks man! but I would like to know if it's possible using LINQ.
Yes, You can use Linq like var stopIds = deserializedObject.stops.Select(p => p.id); to achieve it. Check my answer above to have a better understanding.
Thanks man! Is it possible to have a single LINQ query just to get those 2 values? Stops property I know is an array but it will always only contain just one Stop.
Does this answer your question? Let me know if you need any help. dotnetfiddle.net/H7PdyG
1

Using newtonsoft - also I'm assuming you want a collection of stop ids, not just the one id -

void Main()
{
    var json = "{'fuel_dist': 123, 'fuel_dist_um':'MI', 'id': '62157', 'stops': [{ 'id': 'zz1', latitude: 34.0214}]}";
    JObject jo = JObject.Parse(json);
    var id = (string)jo["id"];
    var arr = jo["stops"] as JArray;
    var stopIds = arr.Select(x=>(string)x["id"]);
    // alternative if it's just one
   var stopId = arr.FirstOrDefault(x=>(string)x["id"]);
}

1 Comment

Hello Matt! yeha, by how the json is formatted I actually assume that it was a collection of stop id's too, but checking the API doc, it will be always just one (don't know why they make it an array) So, for this issue I will be looking for a LINQ query to get just those two values.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.