I am trying to make changes to an C# based opensource json editor which has MIT license. I want to remove the items like ({object}, {Array}) from the Json tree view. here is the link to the Open source Json Editor and here is the link (Clicking on this link will download the JSON-EDITOR)to a editor which i used as a reference for Expected output.
Test.json
{
"TEST JSON" : "JSON",
"JSON":{
"ANIMALS":[
{
"ID":0,
"TYPE":"DOG",
"DOG":{
"TYPE":"RETRIEVER",
"RETRIEVER":{
"NAME":"LEO",
"AGE":3,
"YEARS":[2019 , 2020, 2021],
"WEIGHTS": [2,10,13]
}
},
"REMARKS":{
"ID":1,
"STATUS":"GOOD",
"REFERENCE": {
"SOURCE": "XYZ",
"FIT": 1,
"BMI" : 1
}
}
},
{
"ID":1,
"TYPE":"DOG2",
"DOG2":{
"TYPE":"PUG",
"RETRIEVER":{
"NAME":"HUTCH",
"AGE":4,
"YEARS":[2019 , 2020, 2021, 2022],
"WEIGHTS": [2,3,4,4]
}
},
"REMARKS":{
"ID":1,
"TYPE" : "REFERENCE",
"STATUS":"OK",
"REFERENCE": {
"SOURCE": "XYZ",
"FIT": 1,
"BMI" : 1
}
}
},
{
"ID": 2,
"TYPE": "DIAGNOSTICS",
"STATUS": "ENABLED"
},
{
"ID": 3,
"TYPE": "ORGANISATION",
"ORGANISATION":{
"NAME":"RED CROSS",
"YEAR": 2023
}
}
]
}
}
Like shown in the images below i want to remove elements marked with red to make it look like the image on the right
There are 2 projects inside in the solution JsonEditor and JsonTreeview. There is a function called AfterExpand() in all these files
I’m sure that function is responsible for displaying those unwanted items. so i made the Text string empty in all the files this function is present so the items will be gone.
/// <inheritdoc />
public override void AfterExpand()
{
base.AfterExpand();
Text = $@"[{JArrayTag.Type}]";
// change i made
Text = "";
}
but it seems there are empty spaces being displayed now. Any help would be really appreciated. Thanks in advance.
4
Answers
To remove the items like ({object}, {Array}) from the JSON tree view in C#, you can create a recursive function that traverses the JSON data and removes the nodes that are of type "Object" or "Array". Here’s an example implementation:
Your current solution is not actually modifying the structure of the tree, it is only modifying the text of the nodes that represent object or array.
I understand why you are wanting to do what you want to do: make the tree more compact, and, perhaps, more visually appealing. But, that leads to a loss in "fidelity". Not having the
{object}
and{array}
nodes removes information about what the value of the property actually is: an object or an array (an answer to another question explains it in a bit more detail).This will force your users to take on the cognitive burden of knowing implicitly that, for example,
JSON
is an object because the nodes underneath it are just named properties, and thatANIMALS
in an array because the nodes underneath it are indexed[0]
,[1]
and so on.This is happening at "tree build time" not during the "tree visualization" step. These two bits of code (1, 2)…
TreeNode
that is associated with either aJToken
that’s either aJObject
or aJArray
, which they are. Those are the tokens for{}
(object) and[]
(array) respectively.TreeNode
with the text{object}
or[array]
.If you’re ok with the cognitive burden, you need to change the referenced code to return an
IEnumerable<TreeNode>
instead of a singleTreeNode
.Modifying JsonTreeNodeFactory.cs like so
would do this, but you have to change the call sites to add the enumerable of nodes to the tree (e.g. with
AddRange()
instead of the single add withAdd()
.If you do this, you have to contend with exporting this back to JSON. Somehow you have to "invisibly store" the real context — that the missing node is an object or an array.
I leave that part to you.
Currently
Json Editor
is usingJTokenTreeView
, which is based onTreeview
component. To make it show like the image, you need to modify the code to add the node toTreeView
, mainly inJsonTreeNodeFactory.cs
TreeNode Create(JProperty obj, int depth)
function inJsonTreeNodeFactory.cs
private static void ExpandTreeNodeDepth(JTokenTreeNode node)
inJTokenTreeNode.cs
The main idea is that: Do not create
JObject
orJArray
node if it’s a value of a property. Do not create childJValue
node ofJProperty
.Please be noted that, this code is not full-featured for your question yet. But, you can continue to update for your need:
{Object}
)[index]
when collapse/expandNOTE: because we change the element in treeview, the
Json Editor
may not work as beforeHope this helps!