Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
system.text.json search a dynamic json

I have this JSON string:

string jsonString =
  "{
    "users":[
      {"name":"John", "code":"white", "job":"actor"},
      {"name":"Oliver", "code":"black", "job": "seller"}
     ]
  }"

then deserialize it using:

JsonElement je = JsonDocument.Parse(jsonString).RootElement;
JsonElement.ArrayEnumerator users = je.GetProperty("users").EnumerateArray();

with system.text.json how can I get first JsonElement whose code is "black"? I mean without loops (foreach, ...).

with newtonsoft.json I could simply do this:

dynamic json = JsonConvert.DeserializeObject(jsonString);
dynamic user = json.SelectToken("[?(@.code == 'black')]");
string name = user["name"], job = user["job"];
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

how can I get first JsonElement whose code is "black"

I would create classes that represent your json structure and then deserialize to those classes.

You can create your classes as such:

using System;
using System.Linq;
using System.Collections.Generic;    
using System.Text.Json;
using System.Text.Json.Serialization;

public class People
{   
    [JsonPropertyName("users")]
    public List<User> Users { get; set; }
}

public class User
{   
    [JsonPropertyName("name")]
    public string Name { get; set; }

    [JsonPropertyName("code")]
    public string Code { get; set; }

    [JsonPropertyName("job")]
    public string Job { get; set; }
}

Then to deserialize:

string jsonString = "{\"users\":[{\"name\":\"John\",\"code\":\"white\",\"job\":\"actor\"},{\"name\":\"Oliver\",\"code\":\"black\",\"job\":\"seller\"}]}";
People people = JsonSerializer.Deserialize<People>(jsonString);

Now you can get the first User object where code is black:

 var usr = people.Users.Where(u => u.Code == "black").FirstOrDefault();

Here's the example you can run.

UPDATE PER COMMENT

it is dynamic JSON

You can deserialize your json to JsonElement and then try and get the properties you want.

var people = JsonSerializer.Deserialize<JsonElement>(jsonString);
if(people is JsonElement je && je.TryGetProperty("users", out je)){ 
   // obj will be JsonElement if match is found
   var obj = je.EnumerateArray().Where(je => je.TryGetProperty("code", out je) && je.GetString() == "black").FirstOrDefault();  
   // Get what you need
   var name = obj.TryGetProperty("name", out JsonElement eleName) ? eleName.GetString() : "No name";
   var job = obj.TryGetProperty("job", out JsonElement eleJob) ? eleJob.GetString() : "No job";
   Console.WriteLine(string.Format("{0}\n{1}", name, job));
}

*You can test this code there

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!