NewtonSoft JSON DeserializeXmlNode exception [duplicate] - c#

This question already has answers here:
Census Geocoder JSON output convert to Xml dataset using JSON.net in C#
(2 answers)
Closed 5 years ago.
I have a simple json body
"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"horizontal#Offset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}
So during deserialization using
xmldoc = JsonConvert.DeserializeXmlNode(jsonBody, "widget")
I am getting this exception
The '#' character, hexadecimal value 0x40, cannot be included in a name.
My question is there any naming convention for NewtonSoft JSON parser, as it is not able to convert '#' character? If not, how to get rid of this exception?

As the error says # is not valid character in the xml element's name as per W3C https://www.w3.org/TR/2008/REC-xml-20081126/#NT-Name .You might have to change horizontal#Offset

Related

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);

Newton Json not parsing with bullet characters in the string

The Json file I have got few bullet characters due to which it is not parsing. I tried different ways to replace the bullet characters but no success. Help please. Thanks.
Json file content is below
"items": [ { "type": 202, "path": "C\TestFile.json", "name": " • OptionA?" }]
I tried following with no success.
option 1:
System.IO.File.ReadAllText(#"C:\\Users\\xxx\\Files\\JsonTestFile.txt").Replace(#"\\u2022",string.Empty)
option 2:
System.Text.RegularExpressions.Regex.Replace(strFileContent, "[\\u2022,\\u2023,\\u25E6,\\u2043,\\u2219]\\s\\d", " ");
option 3: converted the file to UTF-8 in notepad++ and tried parsing but it still fails.
{"items": [ { "type": 202, "path": "C\TestFile.json", "name": " • OptionA?" }]}
Above is the correct json but it will not compile because of the value you have for "path".
You have to escape the backslash with another \.. Following deserializes correctly.
{ "items": [ { "type": 202, "path": "C:\\TestFile.json", "name": " • OptionA?" }] }
Console.WriteLine(JObject.Parse(json)["items"][0]["path"].ToString());
//prints
C:\\TestFile.Json

How to remove JSON attributes using c# [duplicate]

This question already has answers here:
Remove fields from JSON dynamically using Json.Net
(2 answers)
Remove specific properties from JSON object
(3 answers)
Closed 3 years ago.
I am trying to find a way of removing JSON properties from an arbitrary JSON document. This is so that I can filter out any data that could be considered sensitive via config.
For example, if I have the below JSON as input, and have an array of property names that I want to filter as ["X","Y"]
{
"X": "some value",
"Y": "another value",
"Z": 123.34,
"Nested": {
"A": 55,
"X": true
},
"selections": [{
"blah": 42,
"X": 55.12
},
{
"blah": 43,
"X": 66.12
},
{
"blah": 44,
"X": 77.12
}
]
}
I would expect the output of the function to be the removal of all X and Y properties whether nested or otherwise:
{
"Z": 123.34,
"Nested": {
"A": 55
},
"selections": [{
"blah": 42
},
{
"blah": 43
},
{
"blah": 44
}
]
}
Am sure this should be possible with NewtonSoft and/or linq?

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