Removing unicode BEL \u0007 special character from json string - c#
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"
}
]
}
Related
How to get the result which has NOT the property with jPath in the newtonsoft?
I want to get the result without some property, can somebody know how to work on json.net? There is an example on the jPath "hello word" document in Goessner articles, { "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } } } If I want to filter all books with isbn number, I can use jToken.SelectTokens("$..book[?(#.isbn)]"), however, if I want to filter all books without isbn number, how can I filter them and whether json.net has this feature.
Edit: Sorry, this only works if the key is present with null, not if it's absent. Got mixed up. For now, you can use $..book[?(#.isbn==null)]. The reason this works is related to another question I answered recently: https://stackoverflow.com/a/69069335/878701
Filter from Pipeline separated string in elasticsearch
I'm using NEST library in .NET for query. I have a mapped a property as CurrentProductStatus(a string). In a document sample record for CurrentProductStatus is like this : "OldStatus|Scanned: [PURCHASE] Recieved|0|#f6f6f6" So i have to filter the result with the second pipeline separated string(ex: "Scanned: [PURCHASE] Recieved"). I have tried with Standard Analyzer. Didnt work for me. QueryContainer query = null; query &= Query<SearchProduct>.Match(m => m.Field(f => f.CurrentProductStatus).Query(searchParameters.ProductStatus).Analyzer("standard")); This is part of my code, this is where i querying. Any idea to do the searching and filtering ??
Using Ingest node with a pipeline looks like it may be a good fit here, to parse separate fields out of the CurrentProductStatus field, and add them to the document that is indexed. You can then query on these fields. The alternative is to define an analyzer that tokenizes the input in a way conducive to your use case. For example, the Standard Analyzer is going to tokenize in the following way GET _analyze { "text": ["OldStatus|Scanned: [PURCHASE] Recieved|0|#f6f6f6"] } --- { "tokens": [ { "token": "oldstatus", "start_offset": 0, "end_offset": 9, "type": "<ALPHANUM>", "position": 0 }, { "token": "scanned", "start_offset": 10, "end_offset": 17, "type": "<ALPHANUM>", "position": 1 }, { "token": "purchase", "start_offset": 20, "end_offset": 28, "type": "<ALPHANUM>", "position": 2 }, { "token": "recieved", "start_offset": 30, "end_offset": 38, "type": "<ALPHANUM>", "position": 3 }, { "token": "0", "start_offset": 39, "end_offset": 40, "type": "<NUM>", "position": 4 }, { "token": "f6f6f6", "start_offset": 42, "end_offset": 48, "type": "<ALPHANUM>", "position": 5 } ] } With the Standard Analyzer applied at query time to Scanned: [PURCHASE] Recieved, GET _analyze { "text": ["Scanned: [PURCHASE] Recieved"] } ---- { "tokens": [ { "token": "scanned", "start_offset": 0, "end_offset": 7, "type": "<ALPHANUM>", "position": 0 }, { "token": "purchase", "start_offset": 10, "end_offset": 18, "type": "<ALPHANUM>", "position": 1 }, { "token": "recieved", "start_offset": 20, "end_offset": 28, "type": "<ALPHANUM>", "position": 2 } ] } Three tokens will match for a match query.
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.
Update Single JSON Value Based on Key
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", }, ] }
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.