Dynamic JSON querying JSON.NET - c#

I have this piece of JSON:
{
"grid": 4,
"rows": [
{
"label": "Full Width",
"name": "FullWidth",
"areas": [
{
"grid": 16,
"hasConfig": false,
"controls": [
{
"value": "Test",
"editor": {
"name": "Headline",
"alias": "headline",
"view": "textstring",
"render": null,
"icon": "icon-coin",
"config": {
"style": "font-size: 36px; line-height: 45px; font-weight: bold",
"markup": "<h1>#value#</h1>"
}
},
"active": false
},
{
"value": "Test",
"editor": {
"name": "Headline",
"alias": "headline",
"view": "textstring",
"render": null,
"icon": "icon-coin",
"config": {
"style": "font-size: 36px; line-height: 45px; font-weight: bold",
"markup": "<h1>#value#</h1>"
}
},
"active": false
}
],
"active": false
}
],
"hasConfig": false,
"id": "e0f6ab79-f562-b86b-f32b-d5520173f0cb",
"active": false
}
]
}
I want to know if any rows (could be multiple rows) have any Controls (Count > 0). I don't need to know what's contained within any of the fields, just if any rows have a control count of > 0 - so just true or false. I've been trying to achieve this using a LINQ query but not getting anywhere fast!
This JSON is dynamic, so I can't deserialize it. Any help would be greatly appreciated! :)

Ended up using the link given by #dbc - this is my code:
JObject sideBar = JObject.Parse(sidebarContentCol.ToString());
IEnumerable<JToken> sidebarControls = sideBar.SelectTokens("$.rows[*].areas[*].controls[*]");
var controls = (bool)(sidebarControls.Count() == 0);

You can do it like this:
string json = #"{
'grid': 4,
'rows': [
{
'label': 'Full Width',
'name': 'FullWidth',
'areas': [
{
'grid': 16,
'hasConfig': false,
'controls': [
{
'value': 'Test',
'editor': {
'name': 'Headline',
'alias': 'headline',
'view': 'textstring',
'render': null,
'icon': 'icon-coin',
'config': {
'style': 'font-size: 36px; line-height: 45px; font-weight: bold',
'markup': '<h1>#value#</h1>'
}
},
'active': false
},
{
'value': 'Test',
'editor': {
'name': 'Headline',
'alias': 'headline',
'view': 'textstring',
'render': null,
'icon': 'icon-coin',
'config': {
'style': 'font-size: 36px; line-height: 45px; font-weight: bold',
'markup': '<h1>#value#</h1>'
}
},
'active': false
}
],
'active': false
}
],
'hasConfig': false,
'id': 'e0f6ab79-f562-b86b-f32b-d5520173f0cb',
'active': false
}
]
}";
var jobject = JObject.Parse(json);
var test = jobject
.Descendants()
.Where(t => t.Type == JTokenType.Property && ((JProperty)t).Name == "controls"
&& t.Ancestors()
.Any(a => a.Type == JTokenType.Property && ((JProperty)a).Name == "rows"))
.Any();
Parse it into a JObject, look for a Descendant named "controls" that has a parent (ancestor) named "rows". test will come out as true if so, false if not.

Related

Chart to display data recorded by month

I have a table where the records have date field column with values like
Date (column 1)
11/1/2019
12/1/2012
12/1/2012
1/3/2013
1/3/2013
1/3/2013
1/3/2013
etc.
Orders (column 2)
project
project
project
project
I want to display a number of records interred by month to chart. I don't know anything about this
Like this
1.First You need to download from nuget.
highcharts.js and
highchartexporting.js
2.Then Declare w where you want show the graph.
like,,
<div id="MonthlyColLine" style="width: 700px; height: 250px; margin: 0 auto"></div>
3.Then add To render Graph
like,
//link your dowloaded higcharts.js and highchartexporting.js to those where you show the graph
<script language="javascript" type="text/javascript">
Highcharts.setOptions({
lang: {
decimalPoint: '.',
thousandsSep: ' '
}
});
$("#MonthlySales").highcharts({
chart: {
type: 'column'
},
title: {
text: ''
},
subtitle: {
text: 'Month Wise Sales',
style: {
color: '#44994a',
fontWeight: 'bold'
}
},
xAxis: {
type: 'category'
},
yAxis: {
title: {
text: 'Amount in Crore.'
}
},
legend: {
enabled: true
},
plotOptions: {
series: {
borderWidth: 0,
dataLabels: {
enabled: true,
format: '{point.y:.1f}'
}
}
},
tooltip: {
headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}</b> of total<br/>'
},
"series": [
{
"name": "Sales",
"colorByPoint": true,
"data": [
{
"name": "Jan",
"y": 18000,//Ypur data here
},
{
"name": "Feb",
"y": 20000,
},
{
"name": "March",
"y": 20000,
},
{
"name": "April",
"y": 2000,
},
{
"name": "May",
"y": 2000,
},
{
"name": "June",
"y": 2000,
},
{
"name": "July",
"y": 2000,
},
{
"name": "Aug",
"y":2000,
},
{
"name": "Sep",
"y": 2000,
},
{
"name": "Oct",
"y": 2000,
},
{
"name": "Nov",
"y": 2000,
},
{
"name": "Dec",
"y": 2000,
}
]
}
]
});
}
</script>

Selenium - json - c#

var pre1 = driver.FindElementByTagName("pre").Text.Replace(#"\", "").Trim();
dynamic root = JsonConvert.DeserializeObject(pre1);
I have this JSON response:
{
"success": true,
"message": null,
"outright": false,
"eventId": 0,
"si": 111,
"leonard": [{
"catalog":[0,0,0,0,0,0],
"edit": 25965112,
"mkilo": {
"888;315;2;3;0": {
"id": 1000,
"description": "Car"
},
"888;316;2;4;0": {
"id": 1001,
"description": "Train"
},
"888;317;2;5;0": {
"id": 1002,
"description": "Airplane"
}
},
"ti": "008000",
"checkin": 254,
"searchCar": {
"id": 1000,
"description": "Car"
}
}],
"ti": 149498
}
verified with jsonlint
root.leonard[0].catalog.Count = 6 ---- > OK
but
root.leonard[0].mkilo.Count = null - -- Why?
I want to read the contents of mkilo.

ElasticSearch Filtered query

I created an elastic search index and the result of a simple search looks like:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 11,
"max_score": 1,
"hits": [
{
"_index": "shop-bestellung",
"_type": "bestellung",
"_id": "dc144b04-8e73-4ea5-9f73-95c01768fd26",
"_score": 1,
"_source": {
"id": "dc144b04-8e73-4ea5-9f73-95c01768fd26",
"bestellnummer": "B-20170302-026",
"shopid": "0143d767-8986-432a-a15d-00e1c4862b24",
"shopname": "DeeDa",
"erstelltVon": "5663bb4b-fc44-46ca-b875-a3487b588b24",
"bestellername": "Max Mann",
"bestelldatum": "2017-01-30T23:00:00Z",
"bestellpositionen": []
}
}
]
}
}
I tried to create a filter which should consits of following three restrictions:
Query text
Date range
Filter on a specific field: "erstelltVon"
My filter only consits of query text and date range:
{
"query":{
"query_string":{
"fields":[
"bestellnummer",
"bestellername",
"bestelldatum",
"erstelltVon",
"bestellpositionen.artikelname",
"bestellpositionen.artikelnummer",
"bestellpositionen.referenznummer"
],
"query":"*"
}
},
"filter": {
"range" : {
"bestelldatum" : {
"gte": "2017-02-04T23:00:00Z",
"lte": "now",
"time_zone": "+01:00"
}
}
}
}
I would like to add the third filter:
"erstelltVon": "5663bb4b-fc44-46ca-b875-a3487b588b24"
How can I do that?
You need to use a boolean filter.
Here is how to use it:
"filter": {
"bool" : {
"must": [
// FIRST FILTER
{
"range" : {
"bestelldatum" : {
"gte": "2017-02-04T23:00:00Z",
"lte": "now",
"time_zone": "+01:00"
}
}
},
{
// YOUR OTHER FILTER HERE
}
]
}
change "must" to "should" if you want to use a OR instead of an AND.

how to JsonConvert.DeserializeObject two data

{
"pagination": {
"next_url": "https://api.instagram.com/v1/users/",
"next_max_id": "98740505131"
},
"meta": {
"code": 200
},
"data": [
{
"attribution": null,
"videos": {
"low_bandwidth": {
"url": "https://scontent.cdninstagram.com/t50.2886-16/12787808_180458939000143_910172373_s.mp4",
"width": 480,
"height": 480
},
"standard_resolution": {
"url": "https://scontent.cdninstagram.com/t50.2886-16/12824263_607003579446709_1011139465_n.mp4",
"width": 640,
"height": 640
},
"low_resolution": {
"url": "https://scontent.cdninstagram.com/t50.2886-16/12787808_180458939000143_910172373_s.mp4",
"width": 480,
"height": 480
}
},
"tags": [],
"type": "video",
"location": null,
"comments": {
"count": 2,
"data": [
{
"created_time": 1457332172,
"text": "什麼東西",
"from": {
"username": "d86241",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/11371189_421316874725117_327631552_a.jpg",
"id": 397355082,
"full_name": "Jhao-wei Hvang"
},
"id": 1200511729352353800
},
{
"created_time": 1457771205,
"text": "😀",
"from": {
"username": "realkikog",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/11820496_1860868347487361_262727492_a.jpg",
"id": 530665716,
"full_name": "K I K O"
},
"id": 1204194607797938400
}
]
},
"filter": "Crema",
"created_time": 1457326470,
"link": "https://www.instagram.com/p/BCo546hPKpA/",
"likes": {
"count": 22,
"data": [
{
"username": "ladyyihan",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/10684228_590857344404221_1064502415_a.jpg",
"id": 38863087,
"full_name": "Yihan"
},
{
"username": "miding_cyh",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/10005160_844941595551352_2014300181_a.jpg",
"id": 226855180,
"full_name": "i😸米丁 Juri"
},
{
"username": "aikoyin1985",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/12093346_523536977821971_1279823341_a.jpg",
"id": 2228728531,
"full_name": "aiko1985"
},
{
"username": "sh1recheungg",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/927866_1722116411358109_618748252_a.jpg",
"id": 416683725,
"full_name": "Sh1reCheungg"
}
]
},
"images": {
"low_resolution": {
"url": "https://scontent.cdninstagram.com/t51.2885-15/s320x320/e15/12446061_1590715141254039_2091776153_n.jpg?ig_cache_key=MTIwMDQ2MzkwMDQ3MDcxNjk5Mg%3D%3D.2",
"width": 320,
"height": 320
},
"thumbnail": {
"url": "https://scontent.cdninstagram.com/t51.2885-15/s150x150/e15/12446061_1590715141254039_2091776153_n.jpg?ig_cache_key=MTIwMDQ2MzkwMDQ3MDcxNjk5Mg%3D%3D.2",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "https://scontent.cdninstagram.com/t51.2885-15/e15/12446061_1590715141254039_2091776153_n.jpg?ig_cache_key=MTIwMDQ2MzkwMDQ3MDcxNjk5Mg%3D%3D.2",
"width": 640,
"height": 640
}
},
"users_in_photo": [],
"caption": {
"created_time": 1457326470,
"text": "測試東西一下",
"from": {
"username": "jiantai.cai",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/1515128_723721747740304_742397288_a.jpg",
"id": 1417858881,
"full_name": "Mars"
},
"id": 1200463904363030800
},
"user_has_liked": false,
"id": "1200463900470716992_1417858881",
"user": {
"username": "jiantai.cai",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/1515128_723721747740304_742397288_a.jpg",
"id": 1417858881,
"full_name": "Mars"
}
}
]
}
i try
var collection = JsonConvert.DeserializeObject(json);
BUT comments data always get null
like this image
enter image description here
how can i model building this json
i want can
foreach(var item in model){
data.comments.data.text
}
to view
Please help ,
I'm using Newtonsoft 8.0.2 and I was able to get what you are asking with this.
string input = File.ReadAllText("C:\\Public\\input.json");
dynamic collection = JsonConvert.DeserializeObject(input);
foreach (var dataItem in collection.data)
{
dynamic comments = dataItem.comments;
foreach (dynamic comment in comments.data)
{
string text = comment.text;
}
}
What you were missing was that you did not iterate through each comment object under your item.

How to count number of JSON elements in an array in C#

I'm worinkg with instagram API and when I'm receiving recent media with any hashtag by this template:
https://api.instagram.com/v1/tags/{hashtag}/media/recent
I'm receiving data like this:
{
"pagination": {
"next_max_tag_id": "any_number",
"deprecation_warning": "next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead",
"next_max_id": "any_number",
"next_min_id": "any_number",
"min_tag_id": "any_number",
"next_url": "https://api.instagram.com/v1/tags/{hashtag}/media/recent?access_token={my_personal_access-token}"
},
"meta": {
"code": 200
},
"data": [
{
"attribution": null,
"tags": [
"any_tag",
"any_tag1",
"any_tag2",
"any_tag3"
],
"type": "image",
"location": null,
"comments": {
"count": 0,
"data": []
},
"filter": "Normal",
"created_time": "any_number",
"link": "any_url",
"likes": {
"count": 0,
"data": []
},
"images": {
"low_resolution": {
"url": "any_url",
"width": 320,
"height": 320
},
"thumbnail": {
"url": "any_url",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "any_url",
"width": 640,
"height": 640
}
},
"users_in_photo": [],
"caption": {
"created_time": "any_number",
"text": "any_content",
"from": {
"username": "any_username",
"profile_picture": "any_url",
"id": "any_number",
"full_name": "any_full_name"
},
"id": "any_number"
},
"user_has_liked": false,
"id": "any_number",
"user": {
"username": "any_username",
"profile_picture": "any_url",
"id": "any_number",
"full_name": "any_full_name"
}
},
and so on.
As You can see, object "data" is an Array, and further we can see object "tags", which is also array. how can I check number elements array of array in C#? i tried like this:
JArray items = (JArray)jsonData["data[0].tags"];
int length = items.Count;
but it doesn't work. I parse JSON like this:
dynamic jsonData = JsonConvert.DeserializeObject<dynamic>(JSON_string);
var token = JToken.Parse(str);
var data = token.Value<JArray>("data");
var tags = data[0].Value<JArray>("tags");
var count = tags.Count;
You can also use a JsonPath:
var token = JToken.Parse(str);
var count = token.SelectTokens("$.data[0].tags[*]").Count();

Categories

Resources