removing properties from complex json object - c#

I am trying to remove some properties which are dynamic input from JObject(converted json to JObject). i can do this on parent elements but not the nested child elements.
INPUT
{
"id": 1,
"name": "something",
"marks": [
{
"pid": 1000,
"sub": "AAA",
"rank": 2
},
{
"pid": 1001,
"sub": "BBB",
"rank": 10
}
]
}
Now i wand to remove the id from parent and pid from each marks property in json. This list is dynamic and may grow in future. data is above provided is an example but the original list(marks in example) contains more than 20 properties.
CODE TRIED (which is deleting only id property from the parent)
string[] props = new string[] { "id", "pid" };
JObject jObject = JsonConvert.DeserializeObject<JObject>(str.ToLower());
if (jObject != null)
{
jObject.Properties()
.Where(attr => props.Contains(attr.Name))
.ToList()
.ForEach(attr => attr.Remove());
}

You can create a function and call it recursively if the nested values are JObject or JArray, like the following code:
1 - Create the main function RemoveIds:
public static void RemoveIds(JObject jObject, string[] props)
{
List<JProperty> jProperties = jObject.Properties().ToList();
for (int i = 0; i < jProperties.Count; i++)
{
JProperty jProperty = jProperties[i];
if (jProperty.Value.Type == JTokenType.Array)
{
RemoveFromArray((JArray)jProperty.Value, props);
}
else if (jProperty.Value.Type == JTokenType.Object)
{
RemoveIds((JObject)jProperty.Value, props);
}
else if (props.Contains(jProperty.Name))
{
jProperty.Remove();
}
}
}
2 - Create simple method RemoveFromArray that call the main function inside a loop:
private static void RemoveFromArray(JArray jArray, string[] props)
{
foreach(JObject jObject in jArray)
{
RemoveIds(jObject, props);
}
}
3 - Call main function in the code like :
JObject jObject = JsonConvert.DeserializeObject<JObject>(json.ToLower());
if (jObject != null)
{
RemoveIds(jObject, new string[] { "id", "pid" });
Console.WriteLine(jObject);
}
4 - Result:
{
"name": "something",
"marks": [
{
"sub": "aaa",
"rank": 2
},
{
"sub": "bbb",
"rank": 10
}
]
}
I hope you find this helpful.

The answer from #Mohammed Sajid was exactly what I was looking for! However, I found that if the property I was wanting to remove was itself an object, it did not get removed. I switched the order of the if/else tests to put the test for removing first which solved my problem. On top of this, since I'm only ever searching for a single property to remove in a large and complex object, I also added an optional findOnce parameter which, if set to true will quit out of the iteration and recursion loops as soon as the property has been removed, thereby saving a few runs around the loop.
public static bool RemoveIds(JObject jObject, string props, in bool findOnce = true)
{
List<JProperty> jProperties = jObject.Properties().ToList();
bool Found = false;
for (int i = 0; i < jProperties.Count; i++)
{
if (findOnce && Found) break;
JProperty jProperty = jProperties[i];
if (props == jProperty.Name)
{
jProperty.Remove();
Found = true;
}
else if (jProperty.Value.Type == JTokenType.Array)
{
Found = RemoveFromArray((JArray)jProperty.Value, props, findOnce);
}
else if (jProperty.Value.Type == JTokenType.Object)
{
Found = RemoveIds((JObject)jProperty.Value, props, findOnce);
}
}
return Found;
}
private static bool RemoveFromArray(JArray jArray, string props, in bool findOnce = true )
{
bool Found = false;
foreach (JObject jObject in jArray)
{
Found = RemoveIds(jObject, props, findOnce);
if( findOnce && Found ) return true;
}
return Found;
}

Related

Convert flat JSON to nested JSON

I have a flat JSON like below (I don't know what to call it, I hope that flat is the right word)
{
"id":12947,
"name.first_name":"Honey",
"address.street.number":"23",
"address.city.code":"LL",
"address.street.name":"Narrow Street",
"address.city.name":"Lalaland",
"name.last_name":"Moon",
"books": [
{
"title":"The long story",
"author.first_name":"Brew",
"author.last_name":"Beating",
"type":"novel"
},
{
"title":"Money and morality",
"author.first_name":"Chris",
"author.last_name":"Mas",
"type":"self-help"
}
]
}
Please notice that the fields are not in sorted order.
I want to convert it into a nested JSON like below:
{
"id":12947,
"name":{
"first_name":"Honey",
"last_name":"Moon"
},
"address":{
"street":{
"number":"23",
"name":"Narrow Street"
},
"city":{
"code":"LL",
"name":"Lalaland"
}
},
"books": [
{
"title":"The long story",
"author": {
"first_name":"Brew",
"last_name":"Beating"
},
"type":"novel"
},
{
"title":"Money and morality",
"author":{
"first_name":"Chris",
"last_name":"Mas"
},
"type":"self-help"
}
]
}
What is a good algorithm to convert it?
I am a C# person, I intend to use Newtonsoft.Json to parse the input JSON to a JObject, then iterate through all fields to check their keys and create nested JObjects. For arrays, I repeat the same process for every array item.
Do you have any better idea?
This is my solution for those who are interested.
public static string ConvertFlatJson(string input)
{
var token = JToken.Parse(input);
if (token is JObject obj)
{
return ConvertJObject(obj).ToString();
}
if (token is JArray array)
{
return ConvertArray(array).ToString();
}
return input;
}
private static JObject ConvertJObject(JObject input)
{
var enumerable = ((IEnumerable<KeyValuePair<string, JToken>>)input).OrderBy(kvp => kvp.Key);
var result = new JObject();
foreach (var outerField in enumerable)
{
var key = outerField.Key;
var value = outerField.Value;
if (value is JArray array)
{
value = ConvertArray(array);
}
var fieldNames = key.Split('.');
var currentObj = result;
for (var fieldNameIndex = 0; fieldNameIndex < fieldNames.Length; fieldNameIndex++)
{
var fieldName = fieldNames[fieldNameIndex];
if (fieldNameIndex == fieldNames.Length - 1)
{
currentObj[fieldName] = value;
continue;
}
if (currentObj.ContainsKey(fieldName))
{
currentObj = (JObject)currentObj[fieldName];
continue;
}
var newObj = new JObject();
currentObj[fieldName] = newObj;
currentObj = newObj;
}
}
return result;
}
private static JArray ConvertArray(JArray array)
{
var resultArray = new JArray();
foreach (var arrayItem in array)
{
if (!(arrayItem is JObject))
{
resultArray.Add(arrayItem);
continue;
}
var itemObj = (JObject)arrayItem;
resultArray.Add(ConvertJObject(itemObj));
}
return resultArray;
}

JSON.net vs XPATH: How to preserve node order in SelectTokens?

XPath 2 states that the nodes order of a selection should be returned in their order in the document.
It looks this is not the case when you SelectTokens(JSONPath) in JSON.Net
When I process the following document
string json = #"
{
""Files"": {
""dir1"": {
""Files"": {
""file1.1.txt"": {
""size:100""},
""file1.2.txt"": {
""size:100""}
}
},
""dir2"": {
""Files"": {
""file2.1.txt"": {
""size:100""},
""file2.2.txt"": {
""size:100""}
}
},
""file3.txt"": {
""size:100""}
}
}";
The order is the following when using JSON.net SelectTokens("$..files.*")
dir1
dir2
file3.txt
file1.1.txt
file1.2.txt
file2.1.txt
file2.2.txt
When I expected the following order (as Xpath //files/*)
dir1
file1.1.txt
file1.2.txt
dir2
file2.1.txt
file2.2.txt
file3.txt
How should I write my query so that I get a List in the XPath order ?
Short of modifying the Json.Net source code, there is not a way that I can see to directly control what order SelectTokens() returns its results. It appears to be using breadth-first ordering.
Instead of using SelectTokens(), you could use a LINQ-to-JSON query with the Descendants() method. This will return tokens in depth-first order. However, you would need to filter out the property names you are not interested in, like "Files" and "size".
string json = #"
{
""Files"": {
""dir1"": {
""Files"": {
""file1.1.txt"": { ""size"": 100 },
""file1.2.txt"": { ""size"": 100 }
}
},
""dir2"": {
""Files"": {
""file2.1.txt"": { ""size"": 100 },
""file2.2.txt"": { ""size"": 100 }
}
},
""file3.txt"": { ""size"": 100 }
}
}";
JObject jo = JObject.Parse(json);
var files = jo.Descendants()
.OfType<JProperty>()
.Select(p => p.Name)
.Where(n => n != "Files" && n != "size")
.ToArray();
Console.WriteLine(string.Join("\n", files));
Fiddle: https://dotnetfiddle.net/yRAev4
If you don't like that idea, another possible solution is to use a custom IComparer<T> to sort the selected properties back into their original document order after the fact:
class JPropertyDocumentOrderComparer : IComparer<JProperty>
{
public int Compare(JProperty x, JProperty y)
{
var xa = GetAncestors(x);
var ya = GetAncestors(y);
for (int i = 0; i < xa.Count && i < ya.Count; i++)
{
if (!ReferenceEquals(xa[i], ya[i]))
{
return IndexInParent(xa[i]) - IndexInParent(ya[i]);
}
}
return xa.Count - ya.Count;
}
private List<JProperty> GetAncestors(JProperty prop)
{
return prop.AncestorsAndSelf().OfType<JProperty>().Reverse().ToList();
}
private int IndexInParent(JProperty prop)
{
int i = 0;
var parent = (JObject)prop.Parent;
foreach (JProperty p in parent.Properties())
{
if (ReferenceEquals(p, prop)) return i;
i++;
}
return -1;
}
}
Use the comparer like this:
JObject jo = JObject.Parse(json);
var files = jo.SelectTokens("$..Files")
.OfType<JObject>()
.SelectMany(j => j.Properties())
.OrderBy(p => p, new JPropertyDocumentOrderComparer())
.Select(p => p.Name)
.ToArray();
Console.WriteLine(string.Join("\n", files));
Fiddle: https://dotnetfiddle.net/xhx7Kk

Find pattern in json with json.net and linq

I'm searching a json file, with the following structure:
{
"objects": [
{
"name": "obj1",
"state": {
"type": 4,
"childs": [
"state": {
"type": 5,
...
The state can contain state as a child until any number of Levels. Now im trying to find all objects containing a certain Patterns of states, e.g. state 4 with child state 5 with child state 2.
My code so far is this.
JObject o = JObject.Parse(System.IO.File.ReadAllText(#"j.json"));
var oObjects=
from p in o["objects"]
where (string)p["state"] == "4"
select (string)p["name"];
How can I expand the code to find all objects containing the search pattern on any Level?
To make it work for indefinite level, then you will need to use a recursive method like the following:
void Main()
{
var str = #"{
""objects"": [
{
""name"": ""obj1"",
""state"": {
""type"": 4,
""childs"": [
{
""state"": {
""type"": 5
}
}
]
}
}
]
}";
var obj = JObject.Parse(str);
GetValidObjects(obj, new string[] { "4", "5" }); // Name list of valid objects
}
And the helper methods defined like:
public IEnumerable<string> GetValidObjects(JObject obj, IEnumerable<string> values)
{
return obj["objects"]
.Where(i => (string)i["state"]["type"] == values.First() && ContainsState((JArray)i["state"]["childs"], values.Skip(1)))
.Select(i => (string)i["name"]);
}
public bool ContainsState(JArray childs, IEnumerable<string> values)
{
if (childs == null)
{
return values.Count() == 0;
}
return childs.Any(i => (string)i["state"]["type"] == values.First() && ContainsState((JArray)i["state"]["childs"], values.Skip(1)));
}
An option could be to convert the json to xml and then use an xpath query to obtain the list of nodes.
string json = System.IO.File.ReadAllText(#"j.json");
XmlDocument document = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
XmlNodeList nodes = document.SelectNodes("//name[../state[type[.=4] and childs/state[type[.=5] and childs/state[type[.=2]]]]]");
You can use SelectTokens for this:
var objects = o.SelectTokens("$.objects[?(#.state.type == 4
&& #.state.childs[*].state.type == 5)].name")
.Select(s => (string)s)
.ToList();

Merge two Json.NET arrays by concatenating contained elements

I have two JToken's that represent JSON arrays of objects and I would like to merge them. JToken has a method Concat but it produces null as result when I try to use it.
Action<JToken> Ok = (x) =>
{
Debug.WriteLine(x);
/* outputs
[
{
"id": 1,
},
{
"id": 2,
}
]
*/
x = (x).Concat<JToken>(x) as JToken;
Debug.WriteLine(x); // null
};
How can I make it work?
Use JContainer.Merge() with MergeArrayHandling.Concat.
This is available starting with Json.NET 6 Release 4. So if your arrays are in a JContainer (e.g. a JObject), this is a simple and robust solution.
Example:
JObject o1 = JObject.Parse(#"{
'FirstName': 'John',
'LastName': 'Smith',
'Enabled': false,
'Roles': [ 'User' ]
}");
JObject o2 = JObject.Parse(#"{
'Enabled': true,
'Roles': [ 'Operator', 'Admin' ]
}");
o1.Merge(o2, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Concat });
string json = o1.ToString();
// {
// "FirstName": "John",
// "LastName": "Smith",
// "Enabled": true,
// "Roles": [
// "User",
// "Operator",
// "Admin"
// ]
// }
JToken.FromObject(x.Concat(x))
I needed same, here's what I came up with
https://github.com/MrAntix/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Linq/MergeExtensions.cs
public static void MergeInto(
this JContainer left, JToken right, MergeOptions options)
{
foreach (var rightChild in right.Children<JProperty>())
{
var rightChildProperty = rightChild;
var leftPropertyValue = left.SelectToken(rightChildProperty.Name);
if (leftPropertyValue == null)
{
// no matching property, just add
left.Add(rightChild);
}
else
{
var leftProperty = (JProperty) leftPropertyValue.Parent;
var leftArray = leftPropertyValue as JArray;
var rightArray = rightChildProperty.Value as JArray;
if (leftArray != null && rightArray != null)
{
switch (options.ArrayHandling)
{
case MergeOptionArrayHandling.Concat:
foreach (var rightValue in rightArray)
{
leftArray.Add(rightValue);
}
break;
case MergeOptionArrayHandling.Overwrite:
leftProperty.Value = rightChildProperty.Value;
break;
}
}
else
{
var leftObject = leftPropertyValue as JObject;
if (leftObject == null)
{
// replace value
leftProperty.Value = rightChildProperty.Value;
}
else
// recurse object
MergeInto(leftObject, rightChildProperty.Value, options);
}
}
}
}

JSON.NET how to remove nodes

I have a json like the following:
{
"d": {
"results": [
{
"__metadata": {
},
"prop1": "value1",
"prop2": "value2",
"__some": "value"
},
{
"__metadata": {
},
"prop3": "value1",
"prop4": "value2",
"__some": "value"
},
]
}
}
I just want to transform this JSON into a different JSON. I want to strip out the "_metadata" and "_some" nodes from the JSON. I'm using JSON.NET.
I just ended up deserializing to JObject and recursively looping through that to remove unwanted fields. Here's the function for those interested.
private void removeFields(JToken token, string[] fields)
{
JContainer container = token as JContainer;
if (container == null) return;
List<JToken> removeList = new List<JToken>();
foreach (JToken el in container.Children())
{
JProperty p = el as JProperty;
if (p != null && fields.Contains(p.Name))
{
removeList.Add(el);
}
removeFields(el, fields);
}
foreach (JToken el in removeList)
{
el.Remove();
}
}
Building off of #[Mohamed Nuur]'s answer, I changed it to an extension method which I think works better:
public static JToken RemoveFields(this JToken token, string[] fields)
{
JContainer container = token as JContainer;
if (container == null) return token;
List<JToken> removeList = new List<JToken>();
foreach (JToken el in container.Children())
{
JProperty p = el as JProperty;
if (p != null && fields.Contains(p.Name))
{
removeList.Add(el);
}
el.RemoveFields(fields);
}
foreach (JToken el in removeList)
{
el.Remove();
}
return token;
}
Here is unit test:
[TestMethod]
public void can_remove_json_field_removeFields()
{
string original = "{\"d\":{\"results\":[{\"__metadata\":{},\"remove\":\"done\",\"prop1\":\"value1\",\"prop2\":\"value2\",\"__some\":\"value\"},{\"__metadata\":{},\"prop3\":\"value1\",\"prop4\":\"value2\",\"__some\":\"value\"}],\"__metadata\":{\"prop3\":\"value1\",\"prop4\":\"value2\"}}}";
string expected = "{\"d\":{\"results\":[{\"prop1\":\"value1\",\"prop2\":\"value2\",\"__some\":\"value\"},{\"prop3\":\"value1\",\"prop4\":\"value2\",\"__some\":\"value\"}]}}";
string actual = JToken.Parse(original).RemoveFields(new string[]{"__metadata", "remove"}).ToString(Newtonsoft.Json.Formatting.None);
Assert.AreEqual(expected, actual);
}
I would create a new data structure with only the required information and copy the data from the first one. Often that is the simpliest approach. Just an idea.
This answer applies if you have a JArray with JTokens, not JObjects:
Here is an example:
string json = "[null, null, \"x\", null, null, null, 0,[],[[\"x\"], null,[0],[\"x\"]]]";
JArray array = JArray.Parse(json);
// Keep first 3 elements, remove the rest
int max = array.Count;
for (int i = 0; i < max - 3; i++)
{
JToken elem = array[3];
array.Remove(elem);
}
json = array.ToString(Newtonsoft.Json.Formatting.None);
Console.WriteLine(json);

Categories

Resources