In the below JSON I am trying to find a condition.Where I want the result only if OrderNumber is not empty or null.
Conditions I have tried. But havent worked for me are:
> $..Item[?(#.OrderNumber)]
> $..Item[?(#.CurrentOrder.OrderNumber)]
Any suggestions will be appreciated.
I am testing my queries here https://jsonpath.curiousconcept.com/
{
"Response": {
"ID": "123456",
"Items": {
"Item": [
{
"CurrentOrder": {
"OrderNumber": "123",
"Status": ""
}
}
]
}
}
}
$..Item[?(#.CurrentOrder.OrderNumber)] is the correct JSONPath for your case.
The tool you used is known to not working correctly. See for example this question.
I have test above query with JSONPath Online Evaluator and for JSON
{
"Response": {
"ID": "123456",
"Items": {
"Item": [
{
"CurrentOrder": {
"OrderNumber": "123",
"Status": ""
}
},
{
"CurrentOrder": {
"OrderNumber": "456",
"Status": ""
}
},
{
"CurrentOrder": {
"Status": ""
}
}
]
}
}
}
it gives correct result:
[
{
"CurrentOrder": {
"OrderNumber": "123",
"Status": ""
}
},
{
"CurrentOrder": {
"OrderNumber": "456",
"Status": ""
}
}
]
Related
Does someone know how to handle JSON string deserialize in C#, which has values string&List simultaneously?
Example:
{
"data": {
"productStream": {
"edges": [{
"node": {
"attributeList": {
"edges": [{
"node": {
"selectAttributeValueTranslations": [{
"value": {
"name": [{
"value": "test"
},
{
"value": null
}
]
}
}]
}
},
{
"node": {
"attribute": {
"code": "example"
},
"imageAttributeValueTranslations": [{
"value": {
"name": "test.jpg",
"extension": "jpg"
}
}]
}
}
]
}
}
}]
}
}
}
And when I'm creating an object to deserialize it I have no idea how to declare this name.
public List Name doesn't work for "name" = "example.jpg". Otherwise public string Name doesn't work for "name"[ value = "example"]
I had to add another class based on JSON Data which contains a value for all attributes and the second class contains a value for only one attribute.
I'm parsing js file containing object values to C# objects. For now - I've converted JS code to JSON and then tried to convert to C# object.
I'm having problem with coming up to idea of how to generate objects in C#. I've tried doing multiple various tries, mostly with Dictionaries (Dictionary<string,[object]). I Googled, visited SO in multiple questions, no success for now - all my ideas resulted in null object.
Important note - I can't change the source of JS, can change anything after that.
Latest objects idea:
public class SingleFarm
{
public List<string> Modules { get; set; }
public List<string> Servers { get; set; }
}
public class SingleEnv
{
public Dictionary<string, SingleFarm> Farms { get; set; }
}
public class FarmsModel
{
public Dictionary<string, SingleEnv> FarmsModel { get; set; }
}
Parsing code:
var farmsText = File.ReadAllText(filePath);
//using Jurassic
var engine = new ScriptEngine();
var result = engine.Evaluate(farmsText);
var json = JSONObject.Stringify(engine, result);
var parsed = JsonConvert.DeserializeObject<FarmsModel>(json);
JS file source:
var environments = {};
environments['ENV1'] = {
"WWW": {
"Modules": [
"module21"
],
"Servers": [
"a-1"
]
}
};
environments['ENV2'] = {
"FARM1": {
"Modules": [
"module41"
],
"Servers": [
"s1",
"s2"
]
},
"FARM2": {
"Modules": [
"module11"
],
"Servers": [
""
]
},
"FARM3": {
"Modules": [
"module1"
],
"Servers": [
""
]
}
};
environments['ENV3'] = {
"FARM1": {
"Modules": [
"module10"
],
"Servers": [
"server1"
]
},
"FARM2": {
"Modules": [
"module22"
],
"Servers": [
""
]
},
"FARM3": {
"Modules": [
"module33"
],
"Servers": [
"server3"
]
}
};
JSON looks as follows:
{
"ENV1": {
"WWW": {
"Modules": [
"module21"
],
"Servers": [
"a-1"
]
}
},
"ENV2": {
"FARM1": {
"Modules": [
"module41"
],
"Servers": [
"s1",
"s2"
]
},
"FARM2": {
"Modules": [
"module11"
],
"Servers": [
""
]
},
"FARM3": {
"Modules": [
"module1"
],
"Servers": [
""
]
}
},
"ENV3": {
"FARM1": {
"Modules": [
"module10"
],
"Servers": [
"server1"
]
},
"FARM2": {
"Modules": [
"module22"
],
"Servers": [
""
]
},
"FARM3": {
"Modules": [
"module33"
],
"Servers": [
"server3"
]
}
}
}
Do you have any ideas?
You shouldn't be trying to serialize dictionaries to objects since it will try to map the property names.
If you use
var parsed = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, SingleFarm>>>(json);
It should work. Fiddle
I have an array like this coming from API response and I want to filter by nested property products to returns product with only id 11
"assets": [
{
"name": "abc",
"products": [
{
"id": "11",
"status": true
},
{
"id": "14",
"status": null
}
]
},
{
"name": "xyz",
"products": [
{
"id": "11",
"status": true
},
{
"id": "28",
"status": null
}
]
},
{
"name": "123",
"products": [
{
"id": "48",
"status": null
}
]
},
{
"name": "456",
"products": [
{
"id": "11",
"status": true
}
]
}
]
the resulting output should look like this,
"assets": [
{
"name": "abc",
"products": [
{
"id": "11",
"status": true
}
]
},
{
"name": "xyz",
"products": [
{
"id": "11",
"status": true
}
]
},
{
"name": "123",
"products": []
},
{
"name": "456",
"products": [
{
"id": "11",
"status": true
}
]
}
]
I'm trying to return filter output with the C# LINQ method. but, getting an error when filtering a nested array with the following method
"Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type"
assets.Select(asset => asset.products.Where(product => product.id == "11")).ToArray();
You can just remove all items from every products array, whose id doesn't equal 11. Parse JSON to JObject, iterate over assets array, find products with id different from 11 and remove them form original array
var json = JObject.Parse(jsonString);
foreach (var asset in json["assets"])
{
var productsArray = asset["products"] as JArray;
if (productsArray == null)
continue;
var productsToRemove = productsArray
.Where(o => o?["id"]?.Value<int>() != 11)
.ToList();
foreach (var product in productsToRemove)
productsArray.Remove(product);
}
Console.WriteLine(json);
Instead of Select and Applying a, Where try to remove the Products that you do not want.
Following is a sample code.
class Program
{
static void Main(string[] args)
{
var json = #"
{
'assets': [
{
'name': 'abc',
'products': [
{
'id': '11',
'status': true
},
{
'id': '14',
'status': null
}
]
},
{
'name': 'xyz',
'products': [
{
'id': '11',
'status': true
},
{
'id': '28',
'status': null
}
]
},
{
'name': '123',
'products': [
{
'id': '48',
'status': null
}
]
},
{
'name': '456',
'products': [
{
'id': '11',
'status': true
}
]
}
]}";
var root = JsonConvert.DeserializeObject<Root>(json);
var assets = root.Assets;
assets.ForEach(a =>
{
a.Products.RemoveAll(p => p.Id != 11);
});
}
}
public partial class Root
{
[JsonProperty("assets")]
public List<Asset> Assets { get; set; }
}
public partial class Asset
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("products")]
public List<Product> Products { get; set; }
}
public partial class Product
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("status")]
public bool? Status { get; set; }
}
To filter the list, you will need to create a new list with products whose ID == YourId. Following linq query does the necessary steps to create the list you want.
Filter out any assets that dont have any product with ID == 11. This is necessary to skip nulls in the new list that will be generated (Where statement)
Create a collection / list of new assets that only have the products whose ID == 11 (Select statement)
var list = assets.Where(x => x.Products.Where(y => y.Id.Equals("11")).Count() > 0)
.Select(asset => {
return new Asset() {
Name = asset.Name,
Products = asset.Products.Where(x => x.Id == "11").ToList()
};
}).ToList();
// Create the RootObject that holds a list of all the arrays if you want.
Rootobject newAssetCollection = new Rootobject() { Assets = list };
Below is the Json that was printed
Console.WriteLine(JsonConvert.SerializeObject(newAssetCollection, Formatting.Indented));
{
"assets": [
{
"name": "abc",
"products": [
{
"id": "11",
"status": true
}
]
},
{
"name": "xyz",
"products": [
{
"id": "11",
"status": true
}
]
},
{
"name": "456",
"products": [
{
"id": "11",
"status": true
}
]
}
]
}
NOTE: An answer was found, see the end of this post and the comments for more information.
ElasticSearch (ver 6) is my kryptonite. I have been trying to add a sorting to a query for far too long and only getting errors in response:
No mapping
Fielddata is disabled on text fields by default. Set fielddata=true ...
when I try to set "fielddata=true" ... I get more errors
[field_sort] unknown field [fielddata], parser not found
I've tried ingore_unmapped and get the same "unknown field" error.
The ES documentation is so generic that it is unhelpful, and nothing here on SO has helped... I've looked at/read after running into various errors
https://stackoverflow.com/a/17051944/10358406
unknown field [dest], parser not found- error coming while reindexing
Elasticsearch: Expected field name but got START_OBJECT
how to set fielddata=true in kibana
So now...
Here is my current query that I use through Postman. I use Nest in my C# project to build this up. I know how to build queries if I can get the json structure right in Postman:
{
"from": 0,
"size": 50,
"aggs": {
"specs": {
"nested": {
"path": "specs"
},
"aggs": {
"names": {
"terms": {
"field": "specs.name",
"size": 10
},
"aggs": {
"specValues": {
"terms": {
"field": "specs.value",
"size": 10
}
}
}
}
}
}
},
"query": {
"constant_score": {
"filter": {
"bool": {
"must": [
{
"term": {
"Id": {
"value": 1
}
}
}
],
"must_not": [
{
"terms": {
"partId": []
}
}
],
"should": [
{
"terms": {
"picCode": [
"b02"
]
}
},
{
"terms": {
"partId": []
}
}
]
}
}
}
}
}
The results I get back are like this:
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 281,
"max_score": 1,
"hits": [
{
"_index": "09812734",
"_type": "part",
"_id": "1234:1",
"_score": 1,
"_source": {
"id": "1234:1",
"partId": 1234,
"mcfCostCenterId": 1,
"oemCode": "ABC",
"oemDescription": "Blah blah blah",
"oemPartCode": "123456",
"picCode": "B02",
"description": "other blah blah",
"isServiceable": false,
"marketingDescription": "this thing does stuff"",
"salesVolume": 0,
"searchSortOrder": 0,
"catalogSortOrders": [],
"specs": [
{
"name": "Color",
"value": "NA"
},
{
"name": "Diameter",
"value": "7.0000"
},
{
"name": "OtherSpec",
"value": "Q"
},
{
"name": "LastSpec",
"value": "FuBar"
}
]
}
}
]
},
"aggregations": {
"specs": {
"doc_count": 18,
"names": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Length",
"doc_count": 4,
"specValues": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "2",
"doc_count": 1
},
{
"key": "3",
"doc_count": 1
},
{
"key": "8",
"doc_count": 1
}
]
}
}
]
}
}
}
}
I need to sort on description or salesVolume in the results.
We have another query that does sorting, so I used that same code for this query, but get the above errors. That code builds up the json as follows:
{
"from": 0,
"size": 50,
"sort": [
{
"foo.bar": {
"missing": "_last",
"order": "desc",
"nested_filter": {
"term": {
"foo.bar": {
"value": "frankenstein"
}
}
},
"nested_path": "_source"
}
}
],
"aggs": {
"specs": {
"nested": {
"path": "specs"
...
As I try to edit that to make it relevant to this search, I just get errors and no results.
EDIT:
So, getting "salesVolume" is pretty easy because it isn't a text field.
{
"from": 0,
"size": 50,
"sort": [
{
"salesVolume": {
"missing": "_last",
"order": "desc"
}
}
],
"aggs": {
...
This sets up a sort order no problem. So, the crux is text... I just can't figure out where to set the fielddata to true without erroring out.
EDIT 2:
In my DTO I have the field already set up with the keyword attribute.
public class MyClass
{
public string Id
[Text]
public string ThisCode
[Keyword]
public string MySortableTextField
... //and so forth
}
The search will still not perform and errors out.
ANSWER:
^^^ The Keyword attribute worked. I was not placing this in the right project. We have a project that builds out all our db indexes and that was still using the Text attribute. Once a indexing was redone then I was able to apply the sort order without errors.
I have following JSON:
{
"cap": [
{
"type": "test"
},
{
"type": "test1"
},
{
"type": "test2",
"bla": "tst"
}
]
}
I want every object that have single property to be one line formated
{ "type": "test" }
so the final JSON should be
{
"cap": [
{ "type": "test" },
{ "type": "test1" },
{
"type": "test2",
"bla": "tst"
}
]
}
How to do this in c#?