Skip to content

Instantly share code, notes, and snippets.

@icodeintx
Created March 18, 2023 13:44
Show Gist options
  • Save icodeintx/14de0adcdf3506d0c0e41d9e64b90bb1 to your computer and use it in GitHub Desktop.
Save icodeintx/14de0adcdf3506d0c0e41d9e64b90bb1 to your computer and use it in GitHub Desktop.
Query json based on config keys
using Newtonsoft.Json.Linq;
using System.Text;
namespace ConsoleApp1;
public class JsonApp
{
private string json = @"{
'Customer': {
'FirstName': 'Amy',
'LastName': 'HomeOwnder'
},
'Address': {
'Street1': '1234 Main Street',
'City': 'Dallas',
'State': 'TX',
'Zip': '77555'
},
'Employer': 'Boeing, Inc',
'Postition': 'Flight Instructor'
}";
public string GetJsonValue(JObject jObj, string path)
{
try
{
if (path.Contains('|'))
{
StringBuilder sb = new StringBuilder();
string[] items = path.Split('|');
foreach (string item in items)
{
sb.Append($"{GetJsonValue(jObj, item)} ");
}
return sb.ToString();
}
var result = jObj.SelectToken(path, true).ToString();
return result;
}
catch (Exception ex)
{
return ($"Error: {ex.Message}");
}
}
public void Run()
{
// mimic configuration settings from database
string config1 = "Customer";
string config2 = "Customer.FirstName";
string config3 = "Address.State";
string config4 = "Employer";
string config5 = "Address.Street1|Address.City|Address.State|Address.Zip";
// create JObject
var jo = JObject.Parse(json);
// query the json based on config settings
var customer = jo[config1];
var firstname = GetJsonValue(jo, config2);
var state = GetJsonValue(jo, config3);
var employer = GetJsonValue(jo, config4);
var address = GetJsonValue(jo, config5).Trim();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment