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?
Related
I'm fairly new to C# and a lot of the libraries, but I'm trying to deserialize a json file that has a bunch of non-dynamic fields followed by a bunch of dynamically named structures with more dynamically sized/named structures underneath them.
Example of json:
"Header": {
"date": "Aug 30 2020",
"time": "14:00:00"
},
...
Other fields that will remain static
...
// Start of fields that can grow/shrink and be added to/deleted from
"Birthday": {
"MessageIndex": 0,
"Info Array Size": 3,
"Info": {
"Date": 30,
"Month": 8,
"Year": 2020
},
},
"Time of Day": {
"MessageIndex": 1,
"Info Array Size": 3,
"Info": {
"Hour": 2,
"Minute": 0,
"Second": 0
},
},
"Height": {
"MessageIndex": 2,
"Info Array Size": 2,
"Info": {
"Feet": 6,
"Inches": 2
},
},
// Continues on for any number of messages
I'm looking to create a tool to be able to parse and document what is in this information without having to add a JsonConverter for each new thing that gets added. Instead I'm looking for something that can
parse any number of items (like "Birthday", "Time of Day", "Height") that have an unknown name and a field named "Info" underneath it with an unknown number of fields and each of those fields name is unknown at the time of running the tool.
I was looking to use JSON.net (or any other available library really), but if that doesn't work I'm fine parsing the file manually. I'd just rather avoid that if possible. Thanks!
I have a json like below and I am trying to create a class for this. What would be the best way to represent this.
For example : A Rule object consists of conditions, like "Any Condition" or "All condition" and they can nested like the below example.
A condition in its simplest form is Class with 3 properties : Fact, Operator, Value.
I am struggling with representing the All and Any Conditions which can be nested. Can some give some idea on how I can represent this in my Rule Class.
{
"conditions": {
"any": [{
"all": [{
"fact": "gameDuration",
"operator": "equal",
"value": 40
}, {
"fact": "personalFoulCount",
"operator": "greaterThanInclusive",
"value": 5
}]
}, {
"all": [{
"fact": "gameDuration",
"operator": "equal",
"value": 48
}, {
"fact": "personalFoulCount",
"operator": "greaterThanInclusive",
"value": 6
}]
}]
}
}
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
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
I have a json object and I want to get the value of :
entities > media > sizes > large > h
Is there a way to get it like XML -> Xpath method?
This is extra lines that is irrelevant to question just because of ...
{
"created_at": "Sun, 01 Jan 2012 17:05:32 +0000",
"entities": {
"user_mentions": [
{
"screen_name": "nurdanterbiyik",
"name": "nurdan",
"id": 264782080,
"id_str": "264782080",
"indices": [
0,
15
]
}
],
"media": [
{
"id": 153522253777219584,
"id_str": "153522253777219584",
"indices": [
44,
64
],
"media_url": "http://p.twimg.com/AiFrrSmCMAAdEID.jpg",
"media_url_https": "https://p.twimg.com/AiFrrSmCMAAdEID.jpg",
"url": "http://t.co/ZwHN9gvO",
"display_url": "pic.twitter.com/ZwHN9gvO",
"expanded_url": "http://twitter.com/emelkiraac/status/153522253773025280/photo/1",
"type": "photo",
"sizes": {
"large": {
"w": 536,
"h": 800,
"resize": "fit"
},
Using JSON.NET you have several ways to read data without having to deserialize your JSON text into objects. Here is a simplified example:
string json = #" {
""created_at"": ""Sun, 01 Jan 2012 17:05:32 +0000"",
""entities"": {
""media"": [{
""type"": ""photo"",
""sizes"": {
""large"": {
""w"": 536,
""h"": 800,
""resize"": ""fit""
}
}
}]
}
}
";
JObject o = JObject.Parse(json);
int h = (int)o["entities"]["media"][0]["sizes"]["large"]["h"];
int h2 = (int)o.SelectToken("entities.media[0].sizes.large.h");
I prefer to think of JSON purely as serialized objects--the first and last thing I want to do with it is to de-serialize it into the objects that I then work with.
I can't tell you the best way to do it in c#, but I've had good luck using JSON.NET
JSON.NET will also let you (quote from website) "LINQ to JSON is good for situations where you are only interested in getting values from JSON, you don't have a class to serialize or deserialize to, or the JSON is radically different from your class and you need to manually read and write from your objects. LINQ to JSON allows you to easily read, create and modify JSON in .NET.", which sounds like what you are trying to do. I've never tried it, but if all you're trying to do is get one piece of data out of that long string, then it sounds like a good alternative.