Update Single JSON Value Based on Key - c#

Experts,
I have a question regarding updating a specific value not based on the index path of the JSON file. For example with the JSON below, with the first set of businesses, I want to update "SubscribeStatus to "true" based on another value like the "1" value of the Key "ID". Is this possible, and how would I achieve this?
High Level Logic:
if (ID == 1)
SubscribeStatus (for first set of businesses) = true
if (ID == 2)
SubscribeStatus (for second set of businesses) = false
JSON:
{"Business":
[
{
"ID": 1,
"Name_Location": "A Name Location",
"Brief_Name_Location": "A Brief Name Location",
"Full_Address": "A Address",
"Latitude": 0.0,
"Longitude": 0.0,
"Phone": 123456789,
"Website": "www.google.com",
"SubscribeStatus":false,
"Logo": "Alogo.png",
},
{
"ID": 2,
"Name_Location": "B Name Location",
"Brief_Name_Location": "B Brief Name Location",
"Full_Address": "B Address",
"Latitude": 1.0,
"Longitude": 1.0,
"Phone": 987654321,
"Website": "www.yahoo.com",
"SubscribeStatus":true,
"Logo": "Blogo.png",
},
]
}

Related

Removing unicode BEL \u0007 special character from json string

I have a JSON response from 3rd party API, and I am trying to convert that into XML file using c# code.
Response field in text view:
"description":"\u0007\tPost-show meet & greet with photo\r\n\u0007\tPremium seat at the front of the auditorium\r\n\u0007\
But while converting this I am getting
System.ArgumentException: '', hexadecimal value 0x07, is an invalid character.
I have tried
Regex : Regex.Replace(content, "[^\x00-\x7F]", String.Empty) and Regex(#"[^\u0000-\u007F]", RegexOptions.Compiled);
Encoding: Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(content));
Ascii check to skip value > 255
breaking into array to replace with empty string or to check every character with System.Xml.XmlConvert.IsXmlChar, but u0007 breaks into 5 different characters,
but no luck so far.
JSON OBJECT to reproduce :
{
"ageRestrictionText": "Under 14s must be accompanied by an adult. We cannot issue refunds to under 14s who are not accompanied by an adult.",
"artists": [],
"canSellViaApi": true,
"categories": [
{
"id": 18,
"name": "Sport",
"childCategories": [
{
"id": 190,
"name": "Other"
}
]
}
],
"currencyCode": "GBP",
"dateOnSale": "2021-09-03T10:00:00",
"dateOnSaleUtc": "2021-09-03T09:00:00Z",
"doorsOpen": "2022-10-17T19:00:00",
"doorsOpenUtc": "2022-10-17T18:00:00Z",
"facilityFee": 0.50,
"ianaTimeZoneName": "Europe/London",
"id": "DF-2026997",
"mainImageUrl": "https://c.ststat.net/content/entimg/tour/an-evening-with-michael-bisping-2029282017-300x300.jpg",
"multiEventBasketAllowed": false,
"name": "An Evening With Michael Bisping",
"narratives": [
{
"content": "1. Your ticket purchase constitutes a personal, revocable license and, at all times, remains the property of the promoters. This ticket must be surrendered to the promoters upon request.<br />2. Your ticket/s are sold by the promoters directly to you the consumer. Any tickets purchased by business or traders in breach of the terms and conditions of ticket sale will be cancelled. By accepting these terms and conditions you confirm that you are a consumer. <br />3. Your ticket/s will immediately become invalid if resold, or offered for resale, unless the sale is through the official re-sale channels as advertised, or ticket agent fan-to-fan exchange (where applicable). Tickets sold via third parties and other unauthorised outlets, including online auction sites, are not valid for admission. The resale of a ticket renders it invalid and may lead to refusal of entry.<br />4. Ticket sales are limited to a maximum of 6 per person/transaction.",
"culture": "en-GB",
"type": "Tour"
}
],
"onSale": true,
"priceGroups": [
{
"description": null,
"id": "PG_DF-2026997_1",
"name": "Standard Pricing",
"prices": [
{
"availableQuantities": [
1,
2,
3,
4
],
"bookingFee": 11.50,
"canSellViaApi": true,
"description": "\tPost-show meet & greet with photo\r\n\tPremium seat at the front of the auditorium\r\n\tExclusive item of merchandise\r\n\tShow ticket",
"faceValue": 110.00,
"id": "DF-2026997_1",
"ticketPrice": 121.50
},
{
"availableQuantities": [
1,
2,
3,
4,
5,
6
],
"bookingFee": 9.60,
"canSellViaApi": true,
"description": "\tPremium seat in the front section of the auditorium\r\n\tExclusive item of merchandise\r\n\tShow ticket",
"faceValue": 70.00,
"id": "DF-2026997_2",
"includedPrices": [],
"isSeated": true,
"maxSell": 6,
"minSell": 1,
"name": "VIP HOT TICKET",
"onSale": true,
"parentPriceId": null,
"priceRestrictions": [],
"quantityMultiple": 1,
"salesChannel": "Web",
"statusText": "ORDER",
"ticketPrice": 79.60
},
{
"availableQuantities": [
1,
2,
3,
4,
5,
6
],
"bookingFee": 5.75,
"canSellViaApi": true,
"description": null,
"faceValue": 35.00,
"id": "DF-2026997_3",
"includedPrices": [],
"isSeated": true,
"maxSell": 6,
"minSell": 1,
"name": "SEATS",
"onSale": true,
"parentPriceId": null,
"priceRestrictions": [],
"quantityMultiple": 1,
"salesChannel": "Web",
"statusText": "ORDER",
"ticketPrice": 40.75
},
{
"availableQuantities": [
1,
2,
3,
4,
5,
6
],
"bookingFee": 3.75,
"canSellViaApi": true,
"description": null,
"faceValue": 25.00,
"id": "DF-2026997_4",
"includedPrices": [],
"isSeated": true,
"maxSell": 6,
"minSell": 1,
"name": "SEATS",
"onSale": true,
"parentPriceId": null,
"priceRestrictions": [],
"quantityMultiple": 1,
"salesChannel": "Web",
"statusText": "ORDER",
"ticketPrice": 28.75
}
],
"salePlatform": "Online"
}
],
"restorationLevy": 0.0,
"seatingPlan": {
"description": "Tyne Theatre & Opera House - Newcastle",
"hasSeatSelectorLayout": true,
"id": 1501,
"imageUrl": "https://c.ststat.net/content/seatingplans/4fedc64d-747d-484b-b8bd-178120deb36a.jpg",
"_links": [
{
"href": "https://ticketing-api.seetickets.com/v1/events/df-2026997/seatingplan",
"method": "GET",
"rel": "self"
}
]
},
"starts": "2022-10-17T20:00:00",
"startsUtc": "2022-10-17T19:00:00Z",
"statusText": "Order",
"textDate": null,
"timeZoneAbbreviation": "BST",
"tour": {
"id": "DF-446241",
"name": "An Evening With Michael Bisping",
"_links": [
{
"href": "https://www.seetickets.com/tour/an-evening-with-michael-bisping",
"method": "GET",
"rel": "self:web"
},
{
"href": "https://ticketing-api.seetickets.com/v1/tours/df-446241",
"method": "GET",
"rel": "self"
}
]
},
"_links": [
{
"href": "https://ticketing-api.seetickets.com/v1/events/df-2026997",
"method": "GET",
"rel": "self"
},
{
"href": "https://www.seetickets.com/event/an-evening-with-michael-bisping/tyne-theatre-opera-house/2026997",
"method": "GET",
"rel": "self:web"
}
]
}

I want to calculate the json file before post to Cosmos db. cosmos will allow max 2mb file size. How it possible in c#?

I have a json looks below. I want to calculate the size of the json in c# that I got from frontend. I will store the json in cosmos db.
My json is:
{
"employeecode": "E1001",
"Employeename": "xyz",
"Address": [
{
"RowID": "1",
"Address1": "58-8",
"Address2": "rachel street",
"City": "Newyork",
"Pin": "100981"
}
],
"Exp": [],
"Education": [
{
"RowID": "1",
"Secondary": "87.8",
"School": "xyz",
"City": "abc"
},
{
"RowID": "2",
"Higher Secondary": "87.8",
"School": "xyz",
"City": "abc"
},
{
"RowID": "3",
"Btech": "8.8",
"college": "xyz",
"City": "abc"
}
]
}
How can I calculte the size of json in c#?
There are different ways to find the string size and it depends on the encoding of your string Unicode or UTF-8. The easiest way is using System.Text.Encoding
If it is encoded with UTF-8
var size_in_MB1 = Math.Round(((decimal)Encoding.UTF8.GetByteCount(json) / 1048576), 2);
And, if it is encoded with Unicode
var size_in_MB2 = Math.Round(((decimal)Encoding.Unicode.GetByteCount(json) / 1048576), 2);

JSON Value (UWP C#)

I writing app for Windows 10.
I have connection with WooCommerce with WooCommerce.Net plugin
Here it is plugin
I have JSON string.
Code:
RestAPI rest = new RestAPI("http://simplegames.com.ua/wp-json/wc/v1/", "ck_9d64c027d2c5f81b8bed3342eeccc6d337be813d", "cs_60697b1e6cbdeb8d62d19e0765e339f8e3334754");
WCObject wc = new WCObject(rest);
//Получение заказов
var orders = await wc.GetOrders()
string products = orders[1].line_items.ToFormattedJsonString();
Debug.WriteLine(products);
I have this JSON in Console
[
{
"id": 72,
"name": "Із лосося",
"sku": "344",
"product_id": 1134,
"variation_id": 0,
"quantity": 1,
"tax_class": "",
"price": 75.00,
"subtotal": 75.00,
"subtotal_tax": 0.00,
"total": 75.00,
"total_tax": 0.00,
"taxes": [
],
"meta": [
]
},
{
"id": 73,
"name": "Італьяно",
"sku": "340",
"product_id": 1138,
"variation_id": 0,
"quantity": 1,
"tax_class": "",
"price": 38.00,
"subtotal": 38.00,
"subtotal_tax": 0.00,
"total": 38.00,
"total_tax": 0.00,
"taxes": [
],
"meta": [
]
}
]
I need to write value from this JSON
for example name to variable.
How I can do this?
It depends on the order line item from where you want to write value from. Assuming int X and int Y to be less than equal to orders.count, orders.line_items.count respectively, You can get the values directly with orders[X].line_items[Y].Propertyname
A better idea would be to create classes representing the business objects and then translating property values into your objects and using it for your application purposes.

How to generate MSSQL Hierarchy ID for each element for a tree object

I've a requirement like dragable tree in my project.
I've successfully converted mssql resultset to json.
But the end user is again dragging and dropping and totally changed the tree structure.
Now I have the below json from the client.
[
{
"id": 1,
"title": "1. dragon-breath",
"items": [
{
"id": 10,
"title": "1. dragon-breath.1",
"items": [
{
"id": 100,
"title": "1. dragon-breath.1.2",
"items": [
{
"id": 1000,
"title": "1. dragon-breath.1.2.3",
"items": [],
"pos": 3
}
],
"pos": 2
},
{
"id": 101,
"title": "1. dragon-breath.1.2",
"items": [],
"pos": 2
}
],
"pos": 1
}
],
"pos": 1
},
{
"id": 102,
"title": "1. dragon-breath.1.2",
"items": [
{
"id": 1020,
"title": "1. dragon-breath.1.2.1",
"items": [],
"pos": 1
}
],
"pos": 2
},
{
"id": 1021,
"title": "1. dragon-breath.1.2.1",
"items": [],
"pos": 1
}
]
From the above json "pos" property is for MSSQL Hierarchyid value.
I am trying to regenerate the hierarchy id of each element, when this json posted back from the client.
I am requesting you people to help me. (spend 2 days no luck)
Following the link you posted to the other SO question, and then a link from a comment, I see you are using the MSSQL hierarchyid data type (hint: it might have helped if you been a bit clearer about that in your question ;)
From the online help I see there is a Parse function, which takes a string.
So you need to traverse your JSON tree structure, building strings for each element in the form /1/1/3/ for example, and pass those back to your database along with the associated node id.

Convert json string to value

I have two json string and also posted here. First json string convert from c# data table using newtonsoft dll. The second one is manual string. If i use the second string means chart displayed well. First one means chart not displayed. I just found the error "value" and "y" like a string in first json string. Kindly help me to change the first one to second one.
1)
[
{
"name": "CHE-CORPORATE",
"value": "42",
"y": "11.8"
},
{
"name": "CHE-TELUGU",
"value": "123",
"y": "10.8"
},
{
"name": "CHE-MALAYALAM",
"value": "13",
"y": "23.8"
}
]
2)
[
{ "name": "CHE-TELUGU",
"value": 123,
"y": 10.8
},
{
"name": "CHE-CORPORATE",
"value": 45,
"y": 40.8
},
{
"name": "CHE-MALAYALAM",
"value": 155,
"y": 12.8
}
]
Just convert the string to number
$.each(data,function(key,val){
val.value=+val.value; // convert the string to number
val.y=+val.y;
});
console.log(data);
Demo

Categories

Resources