Iterate through an ExpandoObject that contains multiple ExpandoObjects - c#

I wondered if it is possible to iterate over an ExpandoObject that contains an array of Expando Objects?
I am currently parsing some JSON with a file structure like the below:
"event":
[{
"name": "BEGIN!",
"count": 1
}],
"context":
{
"customer": {
"greetings":
[
{ "Value1": "Hello" },
{ "Value2": "Bye" }
],
"nicknames": []
}
}
I can retrieve the expando object for 'event' by doing the following:
GenerateDictionary(((ExpandoObject[])dict["event"])[0], dict, "event_");
This is the code for the GenerateDictionary method:
private void GenerateDictionary(System.Dynamic.ExpandoObject output, Dictionary<string, object> dict, string parent)
{
try
{
foreach (var v in output)
{
string key = parent + v.Key;
object o = v.Value;
if (o.GetType() == typeof(System.Dynamic.ExpandoObject))
{
GenerateDictionary((System.Dynamic.ExpandoObject)o, dict, key + "_");
}
else
{
if (!dict.ContainsKey(key))
{
dict.Add(key, o);
}
}
}
}
catch (Exception ex)
{
WritetoLog(itemname, ex);
}
}
I am now totally stuck on how to retrieve all the values in 'context_customer_greetings' as when I attempt to do the below, it will only retrieve the object at context_customer_greetings_value1.
GenerateDictionary(((System.Dynamic.ExpandoObject[])dict["context_customer_greetings"])[0], dict, "context_customer_greetings_");
Is it possible to iterate through an ExpandoObject?
I hope this makes sense, thanking you in advance.

I have now found a solution to this (albeit a simple one!)
I created a new dynamic object and then iterate across that using the same method above.
dynamic s = dict["context_custom_greetings"];
foreach(ExpandoObject o in s)
{
GenerateDictionary((ExpandoObject)o, dict, "context_custom_greetings_");
}

Related

How to recursively descend a System.Text.Json JsonNode hierarchy (equivalent to Json.NET's JToken.DescendantsAndSelf())?

I have an arbitrary JSON document (i.e. without a fixed schema that is known in advance) and I would like to recursively descend it to search for all nodes at any level in the document that match some predicate, so that I can make some necessary modifications. How can I perform such a recursive search using the JsonNode document object model?
Specifics are as follows.
Say I have some JSON such as the following that may contain one or more instances of a property "password" inside:
[
{
"column1": "val_column1",
"column2": "val_column2",
"sheet2": [
{
"sheet2col1": "val_sheet2column1",
"sheet3": [
{
"sheet3col1": "val_sheet3column1",
"password": "password to remove"
}
]
},
{
"sheet2col1": "val_sheet2column1",
"sheet3": [
{
"sheet3col1": "val_sheet3column1"
}
]
}
]
},
{
"column1": "val2_column1",
"column2": "val2_column2",
"password": "password to remove",
"sheet2": [
{
"sheet2col1": "val_sheet2column1",
"sheet3": [
{
"sheet3col2": "val_sheet3column2"
},
null,
null,
19191
],
"password": "password to remove"
},
{
"sheet2col1": "val_sheet2column1",
"sheet3": [
{
"sheet3col2": "val_sheet3column2"
}
]
}
]
}
]
I need to parse it to a JsonNode hierarchy and remove all of the "password" properties wherever they may appear in the JSON hierarchy. With Json.NET, I could parse to JToken and use DescendantsAndSelf():
var root = JToken.Parse(json);
var propertyToRemove = "password";
if (root is JContainer c)
foreach (var obj in c.DescendantsAndSelf().OfType<JObject>().Where(o => o.ContainsKey(propertyToRemove)))
obj.Remove(propertyToRemove);
var newJson = root.ToString();
But JsonNode does not have an equivalent method. How can I do this using System.Text.Json?
Since JsonNode does not have an equivalent to DescendantsAndSelf() we will have to create one ourselves:
public static partial class JsonExtensions
{
public static IEnumerable<JsonNode?> Descendants(this JsonNode? root) => root.DescendantsAndSelf(false);
/// Recursively enumerates all JsonNodes in the given JsonNode object in document order.
public static IEnumerable<JsonNode?> DescendantsAndSelf(this JsonNode? root, bool includeSelf = true) =>
RecursiveEnumerableExtensions.Traverse(
root,
(n) => n switch
{
JsonObject o => o.AsDictionary().Values,
JsonArray a => a,
_ => n.ToEmptyEnumerable(),
}, includeSelf);
/// Recursively enumerates all JsonNodes (including their index or name and parent) in the given JsonNode object in document order.
public static IEnumerable<(JsonNode? node, int? index, string? name, JsonNode? parent)> DescendantItemsAndSelf(this JsonNode? root, bool includeSelf = true) =>
RecursiveEnumerableExtensions.Traverse(
(node: root, index: (int?)null, name: (string?)null, parent: (JsonNode?)null),
(i) => i.node switch
{
JsonObject o => o.AsDictionary().Select(p => (p.Value, (int?)null, p.Key.AsNullableReference(), i.node.AsNullableReference())),
JsonArray a => a.Select((item, index) => (item, index.AsNullableValue(), (string?)null, i.node.AsNullableReference())),
_ => i.ToEmptyEnumerable(),
}, includeSelf);
static IEnumerable<T> ToEmptyEnumerable<T>(this T item) => Enumerable.Empty<T>();
static T? AsNullableReference<T>(this T item) where T : class => item;
static Nullable<T> AsNullableValue<T>(this T item) where T : struct => item;
static IDictionary<string, JsonNode?> AsDictionary(this JsonObject o) => o;
}
public static partial class RecursiveEnumerableExtensions
{
// Rewritten from the answer by Eric Lippert https://stackoverflow.com/users/88656/eric-lippert
// to "Efficient graph traversal with LINQ - eliminating recursion" http://stackoverflow.com/questions/10253161/efficient-graph-traversal-with-linq-eliminating-recursion
// to ensure items are returned in the order they are encountered.
public static IEnumerable<T> Traverse<T>(
T root,
Func<T, IEnumerable<T>> children, bool includeSelf = true)
{
if (includeSelf)
yield return root;
var stack = new Stack<IEnumerator<T>>();
try
{
stack.Push(children(root).GetEnumerator());
while (stack.Count != 0)
{
var enumerator = stack.Peek();
if (!enumerator.MoveNext())
{
stack.Pop();
enumerator.Dispose();
}
else
{
yield return enumerator.Current;
stack.Push(children(enumerator.Current).GetEnumerator());
}
}
}
finally
{
foreach (var enumerator in stack)
enumerator.Dispose();
}
}
}
And now we will be able to do:
var root = JsonNode.Parse(json);
var propertyToRemove = "password";
foreach (var obj in root.DescendantsAndSelf().OfType<JsonObject>().Where(o => o.ContainsKey(propertyToRemove)))
obj.Remove(propertyToRemove);
var options = new JsonSerializerOptions { WriteIndented = true /* Use whatever you want here */ };
var newJson = JsonSerializer.Serialize(root, options);
Demo fiddle #1 here.
Keep in mind the following differences with Json.NET's LINQ to JSON:
The JsonNode returned for a null JSON value (e.g. {"value":null}) will actually be null. LINQ to JSON represents a null JSON value as a non-null JValue with JValue.Type equal to JTokenType.Null.
JsonNode does not have any equivalent to Json.NET's JProperty. The parent of a value in an object will be the object itself. Thus there's no straightforward way to determine the property name of a selected JsonNode property value via the JsonNode document object model.
Thus if you need to search for and modify properties by value (rather than by name), you can use the second extension method DescendantItemsAndSelf() which includes the parent and name or index along with the current node. E.g., to remove all null property values, do the following:
foreach (var item in root.DescendantItemsAndSelf().Where(i => i.name != null && i.node == null).ToList())
((JsonObject)item.parent!).Remove(item.name!);
Demo fiddle #2 here.

Move property child object to root

I have a c# object that natively serializes to
{
"LrsFeature": {
"MEASURE": 1.233242,
"STATION_ID": "brians station",
"NLF_ID": "brians route"
},
"EVENT_ID": "00000000-0000-0000-0000-000000000000",
}
and I want it to be
{
"MEASURE": 1.233242,
"STATION_ID": "brians station",
"NLF_ID": "brians route",
"EVENT_ID": "00000000-0000-0000-0000-000000000000",
}
where all the properties within LrsFeature are added to the root level.
My attempt
var events = JObject.FromObject(LrsEvent);
var attrs = JObject.FromObject(LrsEvent.LrsFeature);
events.Merge(attrs, new JsonMergeSettings
{
MergeArrayHandling = MergeArrayHandling.Union
});
This gives me
{
"MEASURE": 1.233242,
"STATION_ID": "brians station",
"NLF_ID": "brians route",
"LrsFeature": {
"MEASURE": 1.233242,
"STATION_ID": "brians station",
"NLF_ID": "brians route"
},
"EVENT_ID": "00000000-0000-0000-0000-000000000000",
}
I then need to delete the LrsFeature object, but it seems a little bit hacky. I figured JSON.NET might have a more direct method of doing this
You can do what you want like this:
JObject jo = JObject.FromObject(LrsEvent);
JProperty lrs = jo.Property("LrsFeature");
jo.Add(lrs.Value.Children<JProperty>());
lrs.Remove();
string json = jo.ToString();
Fiddle: https://dotnetfiddle.net/zsOQFE
What you're looking to do is deserialize a JSON string and then deep-flatten it. You can do this using the recursive method:
Code
public class JsonExtensions
{
/// <summary>
/// Deeply flattens a json object to a dictionary.
/// </summary>
/// <param name="jsonStr">The json string.</param>
/// <returns>The flattened json in dictionary kvp.</returns>
public static Dictionary<string, object> DeepFlatten(string jsonStr)
{
var dict = new Dictionary<string, object>();
var token = JToken.Parse(jsonStr);
FillDictionaryFromJToken(dict, token, String.Empty);
return dict;
}
private static void FillDictionaryFromJToken(Dictionary<string, object> dict, JToken token, string prefix)
{
if(token.Type == JTokenType.Object)
{
foreach (var property in token.Children<JProperty>())
{
FillDictionaryFromJToken(dict, property.Value, property.Name);
// Uncomment and replace if you'd like prefixed index
// FillDictionaryFromJToken(dict, value, Prefix(prefix, property.Name));
}
}
else if(token.Type == JTokenType.Array)
{
var idx = 0;
foreach (var value in token.Children())
{
FillDictionaryFromJToken(dict, value, String.Empty);
idx++;
// Uncomment and replace if you'd like prefixed index
// FillDictionaryFromJToken(dict, value, Prefix(prefix, idx.ToString()));
}
}
else // The base case
{
dict[prefix] = ((JValue)token).Value; // WARNING: will ignore duplicate keys if you don't use prefixing!!!
}
}
private static string Prefix(string prefix, string tokenName)
{
return String.IsNullOrEmpty(prefix) ? tokenName : $"{prefix}.{tokenName}";
}
}
Usage
var jsonStr = "{\"LrsFeature\":{\"MEASURE\":1.233242,\"STATION_ID\":\"brians station\",\"NLF_ID\":\"brians route\"},\"EVENT_ID\":\"00000000-0000-0000-0000-000000000000\"}";
var dict = JsonExtensions.DeepFlatten(jsonStr);
foreach (var kvp in dict)
Console.WriteLine($"{kvp.Key}={kvp.Value}");
An extension method handling a simple case:
public static JProperty MoveToParent(this JProperty property)
{
if (property is { Parent: JObject { Parent: JProperty { Parent: JObject parent } } })
{
property.Remove();
parent.Add(property);
return property;
}
throw new InvalidOperationException("Could not move to parent.");
}

Issues Iterating through a dynamic object generated from a JSON file

I have a json file that (for the sake of this question) I simplified down:
{
"servername": {
"goodvolumes": [
{
"Name": "vol1",
"State": "online",
"Size": "12.0 TB"
},
{
"Name": "vol2",
"State": "online",
"Size": "10.0 TB"
}
],
"BadVolumes": {
"Name": "badVol",
"State": "offline",
"TotalSize": "120GB"
}
}
}
When this is being read into my C# I have an data object of type System.Collections.Generic.Dictionary<string,object>.
I am then iterating through the object and creating a model object that I am going to be passing to my view.
This is posing a difficulty for me. Here is an example of how I am iterating through the json.
//top level of my JSON - the serverName
foreach(serverName in jsonData)
{
//record the serverName
//second level of my JSON - the notification name
foreach(notification in serverName.Value)
{
//record the notification name
//3rd Level of my JSON - iterating the entries
foreach(entry in notification.Value)
{
//Iterating all the values in an entry
foreach(entryValue in entry)
{
//record values in each entry
}
}
}
}
The issue that I am having is when iterating through the third level if there is only 1 entry.
By the nature of JSON, if a notification type has multiple entries, then inside of my notifications.Value there will be another list of collections. In this case, my code works like a charm.
However, if there is only 1 entry for a notification,notification.value actually contains the list of KeyValuePair for all of the values inside the single entry. So the 3rd level of iteration isn't working. It is actually trying to iterate through the values at that point.
Unfortunately the json I am working with cannot be modified, this is the format that I am going to be receiving it in.
I hope that this accurately explains the issue that I am having. I know where the issue occurs, I am just not sure at all how to get around it.
Firstly, you might consider switching to json.net. If you do, you could deserialize directly to a Dictionary<string, Dictionary<string, List<Dictionary<string, string>>>> using SingleOrArrayConverter<Dictionary<string, string>> from How to handle both a single item and an array for the same property using JSON.net. This also avoids the proprietary date format used by JavaScriptSerializer.
Using JavaScriptSerializer, you will need to know some details of how it deserializes arbitrary JSON data. Specifically, it deserializes JSON objects as IDictionary<string, object> and JSON arrays as some sort of non-dictionary, non-string IEnumerable. The following extension methods implement these checks:
public static class JavaScriptSerializerObjectExtensions
{
public static bool IsJsonArray(this object obj)
{
if (obj is string || obj.IsJsonObject())
return false;
return obj is IEnumerable;
}
public static IEnumerable<object> AsJsonArray(this object obj)
{
if (obj is string || obj.IsJsonObject())
return null;
return (obj as IEnumerable).Cast<object>();
}
public static bool IsJsonObject(this object obj)
{
return obj is IDictionary<string, object>;
}
public static IDictionary<string, object> AsJsonObject(this object obj)
{
return obj as IDictionary<string, object>;
}
/// <summary>
/// If the incoming object corresponds to a JSON array, return it. Otherwise wrap it in an array.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static IEnumerable<object> ToJsonArray(this object obj)
{
if (obj.IsJsonArray())
return obj.AsJsonArray();
return new[] { obj };
}
public static string JsonPrimitiveToString(this object obj, bool isoDateFormat = true)
{
if (obj == null)
return null; // Or return "null" if you prefer.
else if (obj is string)
return (string)obj;
else if (obj.IsJsonArray() || obj.IsJsonObject())
return new JavaScriptSerializer().Serialize(obj);
else if (isoDateFormat && obj is DateTime)
// Return in ISO 8601 format not idiosyncratic JavaScriptSerializer format
// https://stackoverflow.com/questions/17301229/deserialize-iso-8601-date-time-string-to-c-sharp-datetime
// https://msdn.microsoft.com/en-us/library/az4se3k1.aspx#Roundtrip
return ((DateTime)obj).ToString("o");
else
{
var s = new JavaScriptSerializer().Serialize(obj);
if (s.Length > 1 && s.StartsWith("\"", StringComparison.Ordinal) && s.EndsWith("\"", StringComparison.Ordinal))
s = s.Substring(1, s.Length - 2);
return s;
}
}
}
Then you can deserialize as follows:
var jsonData = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(jsonString);
Dictionary<string, Dictionary<string, List<Dictionary<string, string>>>> finalData;
// I could have just done var finalData = ... here. I declared finalData explicitly to makes its type explicit.
finalData =
jsonData.ToDictionary(
p1 => p1.Key,
p1 => p1.Value
.AsJsonObject()
.ToDictionary(
p2 => p2.Key,
p2 => (p2.Value.ToJsonArray().Select(a => a.AsJsonObject())).Select(o => o.ToDictionary(p3 => p3.Key, p3 => p3.Value.JsonPrimitiveToString())).ToList()
));
Now that you have a fully typed hierarchy of dictionaries, you should be able to proceed to create your final model.

JSON.NET serialize JObject while ignoring null properties

I have a JObject which is used as a template for calling RESTful web services. This JObject gets created via a parser and since it's used as a template telling the user what the endpoint schema looks like, I had to figure out a way to preserve all properties, which is why I'm defaulting their values to null. As as example, this is what the object originally looks like:
{
"Foo":{
"P1":null,
"P2":null,
"P3":null,
"P4":{
"P1":null,
"P2":null,
"P3":null,
},
"FooArray":[
{
"F1":null,
"F2":null,
"F3":null,
}
]
},
"Bar":null
}
The user is then able to fill in individual fields as they need, such as Foo.P2 and Foo.P4.P1:
{
"Foo":{
"P1":null,
"P2":"hello world",
"P3":null,
"P4":{
"P1":1,
"P2":null,
"P3":null,
},
"FooArray":[
{
"F1":null,
"F2":null,
"F3":null,
}
]
},
"Bar":null
}
meaning they only care about those two fields. Now I want to serialize this template (JObject) back to a JSON string, but want only those fields that are populated to show up. So I tried this:
string json = JsonConvert.SerializeObject(template,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
Unfortunately, this didn't work. I came across this question and realized that a null value in the object is an actual JToken type and not really a null, which makes sense. However, in this very particular case, I need to be able to get rid of these "unused" fields. I tried manually iterating over nodes and removing them but that didn't work either. Note that the only managed type I'm using is JObject; I don't have a model to convert the object to or define attributes on, since this "template" gets resolved at runtime. I was just wondering if anyone has encountered a problem like this and has any insights. Any help is greatly appreciated!
You can use a recursive helper method like the one below to remove the null values from your JToken hierarchy prior to serializing it.
using System;
using Newtonsoft.Json.Linq;
public static class JsonHelper
{
public static JToken RemoveEmptyChildren(JToken token)
{
if (token.Type == JTokenType.Object)
{
JObject copy = new JObject();
foreach (JProperty prop in token.Children<JProperty>())
{
JToken child = prop.Value;
if (child.HasValues)
{
child = RemoveEmptyChildren(child);
}
if (!IsEmpty(child))
{
copy.Add(prop.Name, child);
}
}
return copy;
}
else if (token.Type == JTokenType.Array)
{
JArray copy = new JArray();
foreach (JToken item in token.Children())
{
JToken child = item;
if (child.HasValues)
{
child = RemoveEmptyChildren(child);
}
if (!IsEmpty(child))
{
copy.Add(child);
}
}
return copy;
}
return token;
}
public static bool IsEmpty(JToken token)
{
return (token.Type == JTokenType.Null);
}
}
Demo:
string json = #"
{
""Foo"": {
""P1"": null,
""P2"": ""hello world"",
""P3"": null,
""P4"": {
""P1"": 1,
""P2"": null,
""P3"": null
},
""FooArray"": [
{
""F1"": null,
""F2"": null,
""F3"": null
}
]
},
""Bar"": null
}";
JToken token = JsonHelper.RemoveEmptyChildren(JToken.Parse(json));
Console.WriteLine(token.ToString(Formatting.Indented));
Output:
{
"Foo": {
"P2": "hello world",
"P4": {
"P1": 1
},
"FooArray": [
{}
]
}
}
Fiddle: https://dotnetfiddle.net/wzEOie
Notice that, after removing all null values, you will have an empty object in the FooArray, which you may not want. (And if that object were removed, then you'd have an empty FooArray, which you also may not want.) If you want to make the helper method more aggressive in its removal, you can change the IsEmpty function to this:
public static bool IsEmpty(JToken token)
{
return (token.Type == JTokenType.Null) ||
(token.Type == JTokenType.Array && !token.HasValues) ||
(token.Type == JTokenType.Object && !token.HasValues);
}
With that change in place, your output would look like this instead:
{
"Foo": {
"P2": "hello world",
"P4": {
"P1": 1
}
}
}
Fiddle: https://dotnetfiddle.net/ZdYogJ
You can prevent the null tokens from being created to begin with by specifying the JsonSerializer with its NullValueHandler set to NullValueHandler.Ignore. This is passed in as a parameter to JObject.FromObject as seen in an answer to the same question you linked to: https://stackoverflow.com/a/29259032/263139.
Brian's answer works. I also came up with another (yet still recursive) way of doing it shortly after posting the question, in case anyone else is interested.
private void RemoveNullNodes(JToken root)
{
if (root is JValue)
{
if (((JValue)root).Value == null)
{
((JValue)root).Parent.Remove();
}
}
else if (root is JArray)
{
((JArray)root).ToList().ForEach(n => RemoveNullNodes(n));
if (!(((JArray)root)).HasValues)
{
root.Parent.Remove();
}
}
else if (root is JProperty)
{
RemoveNullNodes(((JProperty)root).Value);
}
else
{
var children = ((JObject)root).Properties().ToList();
children.ForEach(n => RemoveNullNodes(n));
if (!((JObject)root).HasValues)
{
if (((JObject)root).Parent is JArray)
{
((JArray)root.Parent).Where(x => !x.HasValues).ToList().ForEach(n => n.Remove());
}
else
{
var propertyParent = ((JObject)root).Parent;
while (!(propertyParent is JProperty))
{
propertyParent = propertyParent.Parent;
}
propertyParent.Remove();
}
}
}
}
Using JsonPath we can have a more elegant solution:
jObject.SelectTokens("$..*")
.OfType<JValue>()
.Where(x=>x.Type == JTokenType.Null)
.Select(a => a.Parent)
.ToList()
.ForEach(a => a.Remove());
With a working example here: https://dotnetfiddle.net/zVgXOq
Here's what I was able to come up with. It removes properties that contain only null values. This means that it will handle the case where the property is a scalar value that is null and will also handle the case where there is an array that is all null values. It also removes properties that have no values. This handles the case where the property contains an object that has no child properties. Note, mine uses a JObject which has a Descendents() method which is what made the implementation easy. JToken doesn't have that. My implementation mutates the JObject itself rather than creating a copy of it. Also, it continues removing properties until there aren't any more occurrences. It's a bit more succinct than the other implementations. I don't know how it compares performance-wise.
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Linq;
namespace JsonConsoleApplication
{
class Program
{
static void Main(string[] args)
{
var jo = JObject.Parse(File.ReadAllText(#"test.json"));
Console.WriteLine($"BEFORE:\r\n{jo}");
jo.RemoveNullAndEmptyProperties();
Console.WriteLine($"AFTER:\r\n{jo}");
}
}
public static class JObjectExtensions
{
public static JObject RemoveNullAndEmptyProperties(this JObject jObject)
{
while (jObject.Descendants().Any(jt => jt.Type == JTokenType.Property && (jt.Values().All(a => a.Type == JTokenType.Null) || !jt.Values().Any())))
foreach (var jt in jObject.Descendants().Where(jt => jt.Type == JTokenType.Property && (jt.Values().All(a => a.Type == JTokenType.Null) || !jt.Values().Any())).ToArray())
jt.Remove();
return jObject;
}
}
}
The following is the program output:
BEFORE:
{
"propertyWithValue": "",
"propertyWithObjectWithProperties": {
"nestedPropertyWithValue": "",
"nestedPropertyWithNull": null
},
"propertyWithEmptyObject": {},
"propertyWithObjectWithPropertyWithNull": {
"nestedPropertyWithNull": null
},
"propertyWithNull": null,
"emptyArray": [],
"arrayWithNulls": [
null,
null
],
"arrayWithObjects": [
{
"propertyWithValue": ""
},
{
"propertyWithNull": null
}
]
}
AFTER:
{
"propertyWithValue": "",
"propertyWithObjectWithProperties": {
"nestedPropertyWithValue": ""
},
"arrayWithObjects": [
{
"propertyWithValue": ""
},
{}
]
}

Flattening out a JToken

Suppose I have the following JToken:
#"{
""data"": [
{
""company"": {
""ID"": ""12345"",
""location"": ""Some Location""
},
""name"": ""Some Name""
}
]
}";
I want to pass this token into a FlattenToken function that outputs this JToken:
#"{
""data"": [
{
""company_ID"": ""12345"",
""company_location"": ""Some Location"",
""name"": ""Some Name""
}
]}"
The reason for doing this is so that I can then take the flattened JToken and deserialize it into a DataTable.
I'm getting lost in a jumble of JObjects, JTokens, JProperties, and other JMadness, though. I saw the answer on this post, which was helpful, but I'm still not getting it right.
Here's what I have so far:
public static JToken FlattenToken(JToken token)
{
foreach (JToken topLevelItem in token["data"].Children())
{
foreach (JToken field in topLevelItem.Value<JToken>())
{
foreach (JProperty property in field.Value<JObject>().Properties())
{
field.AddAfterSelf(JObject.Parse(#"{""" + property.Name + "_" + property.Value));
}
field.Remove();
}
}
return token;
}
The first iteration through the outer foreach loop, topLevelItem =
{
"company": {
"ID": "12345"
},
"name": "Some Name"
}
And the first iteration through the second foreach loop, field =
"company": {
"ID": "12345"
}
Looking good so far. But when I hit the innermost foreach loop, I get an exception on the foreach line: "Cannot cast Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JToken."
Not sure what's going on there. I was under the impression that the field.Value call was going to produce a JToken and try to cast it to a JProperty. So where is a JProperty trying to be casted to a JToken, as the error suggests?
Also, this feels like a pretty gross way of flattening out a JToken. Is there a better way?
The hierarchy of objects in Json.NET can be rather deep. A rough guide can be found in this answer.
To solve your problem, you first need an extension method to take the properties of a JObject and return then in a collection with a name prefix:
public static class JsonExtensions
{
public static IEnumerable<KeyValuePair<string, JToken>> FlattenFields(this JObject obj, string prefix)
{
foreach (var field in obj)
{
string fieldName = prefix + "_" + field.Key;
var fieldValue = field.Value;
yield return new KeyValuePair<string, JToken>(fieldName, fieldValue);
}
}
}
Next, you need some recursive tools to iterate through a Json.NET hierarchy and rewrite the collection of properties of selected JObject's:
public static class JsonExtensions
{
public static IEnumerable<T> Yield<T>(this T item)
{
yield return item;
}
public static JToken EditFields(this JToken token, Func<KeyValuePair<string, JToken>, IEnumerable<KeyValuePair<string, JToken>>> editor)
{
if (token == null)
return null;
switch (token.Type)
{
case JTokenType.Array:
return EditFields((JArray)token, editor);
case JTokenType.Object:
return EditFields((JObject)token, editor);
default:
return token;
}
}
static JToken EditFields(JArray array, Func<KeyValuePair<string, JToken>, IEnumerable<KeyValuePair<string, JToken>>> editor)
{
JArray newArray = null;
foreach (var element in array)
{
var newElement = EditFields(element, editor);
if (newElement != null)
{
if (newArray == null)
newArray = new JArray();
newArray.Add(newElement);
}
}
return newArray;
}
static JToken EditFields(JObject obj, Func<KeyValuePair<string, JToken>, IEnumerable<KeyValuePair<string, JToken>>> editor)
{
JObject newObj = null;
foreach (var field in obj)
{
foreach (var newField in editor(field))
{
if (newObj == null)
newObj = new JObject();
newObj[newField.Key] = newField.Value.EditFields(editor);
}
}
return newObj;
}
}
Finally, put these together to create a method that promotes properties of a named JObject property to their parent JObject, prepending the property name plus an underscore:
public static class JsonExtensions
{
public static JToken PromoteNamedPropertiesToParents(this JToken token, string propertyName)
{
return token.EditFields(pair =>
{
if (pair.Key == propertyName && pair.Value is JObject)
{
return ((JObject)pair.Value).FlattenFields(pair.Key);
}
return pair.Yield();
});
}
}
And then, to test:
public static class TestFlatten
{
public static void Test()
{
string jsonString = #"{
""data"": [
{
""company"": {
""ID"": ""12345"",
""location"": ""Some Location""
},
""name"": ""Some Name""
}
]
}";
JObject obj = JObject.Parse(jsonString);
var newObj = (JObject)obj.PromoteNamedPropertiesToParents("company");
Debug.WriteLine(newObj);
}
}
And the output is:
{
"data": [
{
"company_ID": "12345",
"company_location": "Some Location",
"name": "Some Name"
}
]
}
Which is what you want. Please note that this code creates a new JObject hierarchy rather than modifying the original hierarchy.

Categories