-
Notifications
You must be signed in to change notification settings - Fork 295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merging two JSON nodes #48
Comments
Interesting case. No, at the moment there's no real way to merge two nodes. Also if there were, the question is how it should work specifically for arrays or objects. For example if you merge two objects and they have overlapping keys, which should take precedence? The best solution may be to add an AddRange method (like List has) so you can add the node content to another collection node. So yes, as a workaround you have to iterate through the keys yourself
This assumes we talk about two object nodes and we add thekeys / values of node2 to node1. If there are overlapping keys, node2 would take precedence and overwrite those in node1. Note that foreach can be used garbage free since JSONNode has a struct enumerator. Though of course adding new elements to a node would require more memory. Be careful when adding the same JSONNode to several nodes at the same time. The nodes are not duplicated. So both nodes would contain references to the same nodes when simply added like that. At the moment there's no deep copy method, though I may add one in the future. |
I see, thanks for the information. Right now I merge the nodes recursively, to mitigate the problem with overwriting a bit. void MergeNodes(JSONNode to, JSONNode source) {
foreach (var kvp in source) {
if (to.HasKey(kvp.Key)) { MergeNodes(to[kvp.Key], kvp.Value); }
else { to.Add(kvp.Key, kvp.Value); }
}
} But yeah, I didn't think of shared nodes problem. A deep copy would be useful then. |
Hi, I was looking for a way to inject JSON into another JSON in a specific place (at specyfic index if elements are array based) and I came up with such a function - maybe it will be useful to someone.
It works fine for array based source node and only on 1st level. However, idt does not take into account shared names if a given Node has named objects. |
I have my data split over several files and I wondered if there's an easy way to merge the JSONs, or if I have to iterate over all keys myself.
The text was updated successfully, but these errors were encountered: