How to remove a nested property from Json [duplicate] - c#

This question already has answers here:
Getting the error "Cannot add or remove items from Newtonsoft.Json.Linq.JProperty" in Json.net
(2 answers)
Remove fields from JSON dynamically using Json.Net
(2 answers)
Closed 3 years ago.
I want to remove a specific property from json.
The json was like as below and I want to remove "image" attribute.
{
"properties": {
"docker": {
"password": {
"value": "ei"
},
"image": {
"value": ".0.0-11-g1"
}
}
}
}
I tried as below
var obj = JObject.Parse("json");
obj["properties"]["docker"]["image"].Remove();
But I am getting exception

Related

How to deserialize JSON string having a property with different types [duplicate]

This question already has answers here:
How to handle both a single item and an array for the same property using JSON.net
(9 answers)
Closed 2 years ago.
I have a JSON response like the following
{
"result": "success",
"totalresults": "100",
"items": {
"item": [
{
"id": "5812",
"lineitems": {
"lineitem": [
{
"type": "product",
"status": "Active"
}
]
}
},
{
"id": "5",
"lineitems": []
}
]
}
}
While trying to deserialize this with a specific type it throws exception because of the lineitems property. lineitems is an empty array for one item and for the other item it has a property lineitem with an array. I don't have control over this JSON data. Please suggest me how to deserialize this without any error.
Using lineitems as object would help me to deserialize the JSON but then I'll end up having 2 different types in the object field which is not gonna help me either.
If you are sure about "lineitems", You can do the replace policy. Once you get the JSON string from API, Do a condition based replace
json = json.Replace("lineitems: []", "lineitems: null");
Once you do it, Your JSON will be standardized. Then you can do desterilize.

json deserialize items into string format c# [duplicate]

This question already has answers here:
Deserialize JSON with C#
(10 answers)
Closed 2 years ago.
Not that much experience using json, I have read many topics on this but it seems like no one's got the same json structure that I'm working with so i cant seem to get the following items deserialized, basically i will be populating a listbox with all bucketName in the list as shown below, the json gets returned from a web request.
This currently holds all the json data:
var responseString = new StreamReader(responseList.GetResponseStream()).ReadToEnd();
And this is the content it holds:
{
"buckets":
[
{
"accountId": "someweirdid",
"bucketId": "4a48fe8875c6214145260818",
"bucketInfo": {},
"bucketName" : "Kitten-Videos",
"bucketType": "allPrivate",
"lifecycleRules": []
},
{
"accountId": "uuhhmthisisarandomid",
"bucketId" : "5b232e8875c6214145260818",
"bucketInfo": {},
"bucketName": "Puppy-Videos",
"bucketType": "allPublic",
"lifecycleRules": []
},
{
"accountId": "ahhhanotherid",
"bucketId": "87ba238875c6214145260818",
"bucketInfo": {},
"bucketName": "Vacation-Pictures",
"bucketType" : "allPrivate",
"lifecycleRules": []
}
]
}
I need the bucketName item to be populated in a listbox, please any help, thanks.
Try converting json object to C# and create a list which can be used to populate listbox
using Newtonsoft.Json;
...
var result = JsonConvert.DeserializeObject<T>(json);

Ways to parse Json recursive [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
My problem is that I need to parse json to another json format, I need to do that recursive. I have been tried diferents way but any works fine for me.
I would like to know more ways for test.
Thanks!
{
"items": {
"itemName": {
"type": "type",
"properties": {
"item1": {
"type": 1,
"isValid": true
},
"item2": {
"type": 1,
"isValid": true
}
}
}
}
}
And I need to make this
{
"items":{
"item1": 1,
"item2": 1
}
}
You can try with JToken to read the JSON and JsonConvert to convert the object to desired JSON
using (StreamReader r = new StreamReader(filepath))
{
var inputString = r.ReadToEnd();
JToken outer = JToken.Parse(inputString);
JObject inner = outer["items"]["itemName"]["properties"].Value<JObject>();
List<string> keys = inner.Properties().Select(p => p.Name).ToList();
var items = new ExpandoObject() as IDictionary<string, Object>;
foreach (string k in keys)
{
items.Add(k, Convert.ToInt32(outer["items"]["itemName"]["properties"][k]["type"]));
}
Console.WriteLine(JsonConvert.SerializeObject(new { items = items }));
}
output
{"items":{"item1":1,"item2":2}}

C# Select From Json List Using Where Clause [duplicate]

This question already has answers here:
Find an item in a list by LINQ
(14 answers)
Closed 4 years ago.
I have a json list and I have a type named post. I want to use it for search.
I want to get a list or one object with a query. Is that possible ?
Example part of json
{
"Post": [
{
"id":"22",
"text":"Dream Big",
"img":"a2ca3cf9-664e-4d92-80f1-df20e971b7c0.jpg",
"catid":"12",
"meta_title":"Dream Big Design",
"content":"some text",
"user_id":"5556",
}
{
"id":"24",
"text":"Handmade Resin",
"img":"423233-971b7c0.jpg",
"catid":"7",
"meta_title":"Handmade Resin",
"content":"some text",
"user_id":"1256",
}
]
}
I want to select id = 23 or name like 'handmade'.
I tried with this code but it did not work
string json = System.IO.File.ReadAllText(path + "output.json");
var serializer = new JavaScriptSerializer();
Post post = JsonConvert.DeserializeObject<Post>(json);
you can use JObject in the C# that can parse the Json object and then you can fire your Select query.
JObject jObject = JObject.Parse(your Json object in string);

How do I get json.net to exclude nulls when deseralizing a collection? [duplicate]

This question already has answers here:
How to ignore a property in class if null, using json.net
(17 answers)
Closed 5 years ago.
I have JSON that I am getting back that potentially gets back nulls as part of the values. How can I, or is there even a way, to exclude those nulls from the collection?
{
"id": "5551212",
"from": {
"name": "Message creator",
"start_time": "2011-10-21T22:00:00",
"end_time": "2011-10-23T17:00:00",
"location": "area 51",
"id": "2121212122"
},
"to": {
"data": [
{
"name": "Jay-Z",
"id": "77777"
},
{
"name": "Bill Murray",
"id": "88888"
},
null,
{
"name": "Anthony Hopkins",
"id": "99999"
}
]
},
"message": "Some message from somewhere",
"updated_time": "2011-09-19T23:53:51+0000",
"unread": 1,
"unseen": 0
}
Notice between Bill Murray and Anthony Hopkins the null that is returned. Thanks.
You can simply decorate properties/fields that can be null with
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string Name { get; set; }
For more info on how to reduce json size: http://james.newtonking.com/archive/2009/10/23/efficient-json-with-json-net-reducing-serialized-json-size.aspx
I would use convert method to get an XML string.
// jsonString is populated from your....
XmlNode xmlNode = JsonConvert.DeserializeXmlNode(jsonString);
The null value in XML would be like:
...<data></data>...
which can be easily removed by string replacement or XML filtering.

Categories