I have 3 levels of JSON object and trying to sort based on one of the inner-element.
Here is the sample JSON.
public class Program
{
public static void Main()
{
string myJSON = #"
{
""items"": ""2"",
""documents"": [
{
""document"": {
""libraryId"": ""LIB0001"",
""id"": ""100"",
""elements"": {
""heading"": {
""elementType"": ""text"",
""value"": ""My Heading 1""
},
""date"": {
""elementType"": ""datetime"",
""value"": ""2020-07-03T20:30:00-04:00""
}
},
""name"": ""My Name 1 "",
""typeId"": ""10ed9f3f-ab41-45a9-ba24-d988974affa7""
}
},
{
""document"": {
""libraryId"": ""LIB0001"",
""id"": ""101"",
""elements"": {
""heading"": {
""elementType"": ""text"",
""value"": ""My Heading 2""
},
""date"": {
""elementType"": ""datetime"",
""value"": ""2020-07-03T20:30:00-04:00""
}
},
""name"": ""My Name 2"",
""typeId"": ""10ed9f3f-ab41-45a9-ba24-d988974affa7""
}
}
]
}";
JObject resultObject = JObject.Parse(myJSON);
var sortedObj = new JObject(
resultObject.Properties().OrderByDescending(p => p.Value)
);
string output = sortedObj.ToString();
Console.WriteLine(output);
}
}
I would like to sort based the "date" field. Appreciate any help.
You can replace documents json array with sorted one using json path to sort it:
resultObject["documents"] = new JArray(resultObject["documents"]
.Children()
.OrderBy(p => p.SelectToken("$.document.elements.date.value").Value<DateTime>()));
Console.WriteLine(resultObject.ToString());
Or using indexer access:
resultObject["documents"] = new JArray( resultObject["documents"]
.Children()
.OrderBy(p => p["document"]["elements"]["date"]["value"].Value<DateTime>()));
Related
I was trying to insert nested Json string to existing Json string . Here's my json sample
JSON #1:
{
"Products":[
{
productId:1,
productName:"product1"
}
]
}
JSON #2:
{
"Brand":[
{
brandId:1,
brandName:"brand1"
}
]
}
Should be output
{
"Products":[
{
productId:1,
productName:"product1",
"Brand":[
{
brandId:1,
brandName:"brand1"
}
]
}
]
}
This is done thru c# code.Thankyou in advance
try this
using Newtonsoft.Json;
var jsonObj1=JObject.Parse(json1);
var jsonObj2=JObject.Parse(json2);
jsonObj1["Products"][0]["Brand"]=jsonObj2["Brand"];
string mergedJson=jsonObj1.ToString();
Console.WriteLine(mergedJson);
result
{
"Products": [
{
"productId": 1,
"productName": "product1",
"Brand": [
{
"brandId": 1,
"brandName": "brand1"
}
]
}
]
}
I am needing to produce this JSON string with C#:
{
"in0": {
"children": [
{
"ValueObjectDescriptor": {
"fields": [
{
"FieldDescriptor": {
"name": "length",
"xpath": "#lenth"
}
},
{
"FieldDescriptor": {
"name": "height",
"xpath": "#height"
}
},
{
"FieldDescriptor": {
"name": "width",
"xpath": "#width"
}
}
],
"objectName": "Job",
"limit": 1,
"xpathFilter": "#openJob = 'true'"
}
}
]
}
}
Here is my code:
static string BuildJsonString()
{
var json = new
{
in0 = new
{
children = new
{
ValueObjectDescriptor = new
{
fields = new
{
FieldDescriptor = new
{
name = "length",
xpath = "#length",
},
FieldDescriptor = new
{
name = "height",
xpath = "#height",
},
FieldDescriptor3 = new
{
name = "width",
xpath = "#width",
},
objectName = "Job",
limit = "1",
xpathFilter = "#openJob='true'"
}
}
}
}
};
var jsonFormatted = JsonConvert.SerializeObject(json, Newtonsoft.Json.Formatting.Indented);
return jsonFormatted.ToString();
The issue I am having is that the compiler doesn't like me using "FieldDescriptor" multiple times, I get the error "An anonymous type cannot have multiple properties with the same name".
I am very new to JSON, so any advice would be greatly appreciated.
Note that not only is having multiple fields with the same name invalid C# code, having duplicate keys in the same object is also invalid JSON. You can be sure that there is no JSON that would need you to write duplicate field names to serialise it.
The "FieldDescriptor" JSON keys are actually part of different JSON objects, and these JSON objects are all in a JSON array under the key "fields":
[
{
"FieldDescriptor": {
"name": "length",
"xpath": "#lenth"
}
},
{
"FieldDescriptor": {
"name": "height",
"xpath": "#height"
}
},
{
"FieldDescriptor": {
"name": "width",
"xpath": "#width"
}
}
]
The [ ... ] denotes the array, and each pair of { ... } denotes a JSON object. So you should create an (implicitly typed) array of anonymous objects, each one with the FieldDescriptor property, rather than one object having all three of the proeperties:
fields = new[] // <--- create an array
{
new {
FieldDescriptor = new
{
name = "length",
xpath = "#length",
}},
new { // notice the new pairs of curly braces
FieldDescriptor = new
{
name = "height",
xpath = "#height",
}}, // here's the closing brace
new {
FieldDescriptor3 = new
{
name = "width",
xpath = "#width",
}},
objectName = "Job",
limit = "1",
xpathFilter = "#openJob='true'"
}
It looks like in the json "fields" is an array of objects where each object contains a "FieldDescriptor" field. In your C# code you are creating "fields" as an object with multiple fields not an array of objects.
I've a sample company JSON document structure like this:
[
{
"id": "id1",
"name": "company1",
"employees": [
{
"name": "employee1",
"education": "education1"
},
{
"name": "employee2",
"education": "education2"
}
]
}
]
and I'm doing queries like this:
GET companies/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"employees.education": {
"value": "education1"
}
}
},
{
"term": {
"employees.education": {
"value": "education2"
}
}
}
]
}
}
}
The query is build using NEST:
var filters = new List<Func<QueryContainerDescriptor<Company>, QueryContainer>>();
foreach (var education in educationsToFilter)
{
filters.Add(fq => fq.Term(f => f.Employees.Suffix("education"), education));
}
var searchResponse = _client.Search<Company>(s => s
.Query(q => q
.Bool(bq => bq
.Filter(filters)
)
)
);
Is there a better way of finding the Term Field instead of using the Suffix-method? I would like a more type safe way.
You can resolve nested field name with this lambda expression:
fq.Term(f => f.Employees.FirstOrDefault().Education, education)
Hope that helps.
I'd like to use moreLikeThis query on Elasticsearch using NEST library and give different boost values for each match.
var moreLikeThis = _elastic.Search<Report>(s => s
.From(0)
.Size(10)
.Query(q => q
.Filtered(f => f
.Query(fq => fq
.Dismax(dmx => dmx
.TieBreaker(0.7)
.Queries(qr => qr
.MoreLikeThis(mlt => mlt
.OnFields(of => of.Title.Suffix("stemmed"))
.MinTermFrequency(1)
.MaxQueryTerms(12)
.Boost(20)
.Documents(docs => docs
.Document() // This is the part where I'm stuck
)
), qr => qr
.MoreLikeThis(mlt => mlt
.OnFields(of => of.Title.Suffix("synonym"))
.MinTermFrequency(1)
.MaxQueryTerms(12)
.Boost(10)
)
)
)
)
)
)
);
This the query I'm stuck at. I can write this easily in raw JSON format, but that's not what I'm aiming for. I'm quite new in both C# and NEST and do not know how to pass documentId there.
That's my class, if it helps at all :
[ElasticType(IdProperty = "_id", Name = "reports")]
public class Report
{
[ElasticProperty(Name = "_id", Type = FieldType.String)]
public string _id { get; set; }
[ElasticProperty(Name = "Title", Type = FieldType.String)]
public string Title { get; set; }
}
And that's the query I'm using as JSON and it works just fine.
{
"from": 0,
"size": 10,
"fields": ["Title"],
"query": {
"filtered": {
"query": {
"dis_max": {
"tie_breaker": 0.7,
"queries": [
{
"more_like_this": {
"fields": ["Title.stemmed"],
"docs": [
{
"_index": "test",
"_type": "reports",
"_id": "68753"
}
],
"min_term_freq": 1,
"max_query_terms": 12
}
}, {
"more_like_this": {
"fields": ["Title.synonym"],
"docs": [
{
"_index": "test",
"_type": "reports",
"_id": "68753"
}
],
"min_term_freq": 1,
"max_query_terms": 12
}
}
]
}
}
}
}
}
Documentation in NEST doesn't explain or give a basic idea how this is done.
Thank you.
As stated in comments this was a bug and will be fixed together with 1.5.2 NEST release.
I have some Json response from server, for example:
{"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 50.4639653,
"lng" : 30.6325177
},
"southwest" : {
"lat" : 50.4599625,
"lng" : 30.6272425
}
},
"copyrights" : "Map data ©2013 Google",
"legs" : [
{
"distance" : {
"text" : "1.7 km",
"value" : 1729
},
"duration" : {
"text" : "4 mins",
"value" : 223
},
And I want to get the value of token 'text' from
"legs" : [
{
"distance" : {
"text" : "1.7 km",
"value" : 1729
},
which is string with value "1.7 km".
Question: is there any build-in function in NewtonsoftJson lib which can be look like:
public string(or JToken) GetJtokenByName(JObject document, string jtokenName)
or do I need to implement some recursive method which will search JToken by name in all JTokens and JArrays in JObject?
If you are looking for a very specific token and know the path to it, you can navigate to it easily using the built-in SelectToken() method. For example:
string distance = jObject.SelectToken("routes[0].legs[0].distance.text").ToString();
If you need to find all occurences of a token with a given name in your JSON, no matter where they occur, then yes you'd need a recursive method. Here is one that might do the trick:
public static class JsonExtensions
{
public static List<JToken> FindTokens(this JToken containerToken, string name)
{
List<JToken> matches = new List<JToken>();
FindTokens(containerToken, name, matches);
return matches;
}
private static void FindTokens(JToken containerToken, string name, List<JToken> matches)
{
if (containerToken.Type == JTokenType.Object)
{
foreach (JProperty child in containerToken.Children<JProperty>())
{
if (child.Name == name)
{
matches.Add(child.Value);
}
FindTokens(child.Value, name, matches);
}
}
else if (containerToken.Type == JTokenType.Array)
{
foreach (JToken child in containerToken.Children())
{
FindTokens(child, name, matches);
}
}
}
}
Here is a demo:
class Program
{
static void Main(string[] args)
{
string json = #"
{
""routes"": [
{
""bounds"": {
""northeast"": {
""lat"": 50.4639653,
""lng"": 30.6325177
},
""southwest"": {
""lat"": 50.4599625,
""lng"": 30.6272425
}
},
""legs"": [
{
""distance"": {
""text"": ""1.7 km"",
""value"": 1729
},
""duration"": {
""text"": ""4 mins"",
""value"": 223
}
},
{
""distance"": {
""text"": ""2.3 km"",
""value"": 2301
},
""duration"": {
""text"": ""5 mins"",
""value"": 305
}
}
]
}
]
}";
JObject jo = JObject.Parse(json);
foreach (JToken token in jo.FindTokens("text"))
{
Console.WriteLine(token.Path + ": " + token.ToString());
}
}
}
Here is the output:
routes[0].legs[0].distance.text: 1.7 km
routes[0].legs[0].duration.text: 4 mins
routes[0].legs[1].distance.text: 2.3 km
routes[0].legs[1].duration.text: 5 mins
This is pretty simple using the json paths and the SelectTokens method on JToken. This method is pretty awesome and supports wilds cards such as the following:
jObject.SelectTokens("routes[*].legs[*].*.text")
Check out this sample code:
private class Program
{
public static void Main(string[] args)
{
string json = GetJson();
JObject jObject = JObject.Parse(json);
foreach (JToken token in jObject.SelectTokens("routes[*].legs[*].*.text"))
{
Console.WriteLine(token.Path + ": " + token);
}
}
private static string GetJson()
{
return #" {
""routes"": [
{
""bounds"": {
""northeast"": {
""lat"": 50.4639653,
""lng"": 30.6325177
},
""southwest"": {
""lat"": 50.4599625,
""lng"": 30.6272425
}
},
""legs"": [
{
""distance"": {
""text"": ""1.7 km"",
""value"": 1729
},
""duration"": {
""text"": ""4 mins"",
""value"": 223
}
},
{
""distance"": {
""text"": ""2.3 km"",
""value"": 2301
},
""duration"": {
""text"": ""5 mins"",
""value"": 305
}
}
]
}]}";
}
}
And here's the output:
routes[0].legs[0].distance.text: 1.7 km
routes[0].legs[0].duration.text: 4 mins
routes[0].legs[1].distance.text: 2.3 km
routes[0].legs[1].duration.text: 5 mins
In case you want all values of a property, regardless of where it occurs, here is an alternative to recursion as described by #brian-rogers, using SelectToken as suggested by #mhand:
To get all values of duration.text, you can use SelectToken and Linq:
var list = jObject.SelectTokens("$..duration.text")
.Select(t => t.Value<string>())
.ToList();
More info: Querying JSON with SelectToken