Having dealt a lot with tag json in scripting, it occured to me that it would be easier to deal with, especially for large numbers of tags, if the json was formatted differently by replacing arrays with dictionaries when housing tags and folders. What defines the tag structure is the names, not the positions of them in an array.
For example:
JSON Now:
{
"name": "New Folder",
"tagType": "Folder",
"tags": [
{
"name": "Area 2",
"tagType": "Folder",
"tags": [
{
"name": "Sub Area 2",
"tagType": "Folder",
"tags": [
{
"valueSource": "memory",
"name": "Tag 1",
"value": 10,
"tagType": "AtomicTag"
}
]
},
{
"name": "Sub Area 1",
"tagType": "Folder",
"tags": [
{
"valueSource": "memory",
"name": "Tag 3",
"value": 20,
"tagType": "AtomicTag"
},
{
"valueSource": "memory",
"name": "Tag 1",
"value": 10,
"tagType": "AtomicTag"
},
{
"valueSource": "memory",
"name": "Tag 2",
"value": 20,
"tagType": "AtomicTag"
}
]
}
]
},
{
"name": "Area 1",
"tagType": "Folder",
"tags": [
{
"name": "Sub Area 1",
"tagType": "Folder",
"tags": [
{
"valueSource": "memory",
"name": "Tag 1",
"value": 10,
"tagType": "AtomicTag"
},
{
"valueSource": "memory",
"name": "Tag 2",
"value": 20,
"tagType": "AtomicTag"
}
]
},
{
"name": "Sub Area 2",
"tagType": "Folder",
"tags": [
{
"valueSource": "memory",
"name": "Tag 1",
"value": 10,
"tagType": "AtomicTag"
}
]
}
]
}
]
}
My new JSON:
{
"name": "New Folder",
"tagType": "Folder",
"tags": {
"Area 2": {
"name": "Area 2",
"tagType": "Folder",
"tags": {
"Sub Area 2": {
"name": "Sub Area 2",
"tagType": "Folder",
"tags": {
"Tag 1": {
"valueSource": "memory",
"name": "Tag 1",
"value": 10,
"tagType": "AtomicTag"
}
}
},
"Sub Area 1": {
"name": "Sub Area 1",
"tagType": "Folder",
"tags": {
"Tag 3": {
"valueSource": "memory",
"name": "Tag 3",
"value": 20,
"tagType": "AtomicTag"
},
"Tag 1": {
"valueSource": "memory",
"name": "Tag 1",
"value": 10,
"tagType": "AtomicTag"
},
"Tag 2": {
"valueSource": "memory",
"name": "Tag 2",
"value": 20,
"tagType": "AtomicTag"
}
}
}
}
},
"Area 1": {
"name": "Area 1",
"tagType": "Folder",
"tags": {
"Sub Area 1": {
"name": "Sub Area 1",
"tagType": "Folder",
"tags": {
"Tag 1": {
"valueSource": "memory",
"name": "Tag 1",
"value": 10,
"tagType": "AtomicTag"
},
"Tag 2": {
"valueSource": "memory",
"name": "Tag 2",
"value": 20,
"tagType": "AtomicTag"
}
}
},
"Sub Area 2": {
"name": "Sub Area 2",
"tagType": "Folder",
"tags": {
"Tag 1": {
"valueSource": "memory",
"name": "Tag 1",
"value": 10,
"tagType": "AtomicTag"
}
}
}
}
}
}
}
E.g. instead of snippet:
"tags": [
{
"name": "Area 2",
it becomes:
"tags": { <<<==
"Area 2": { <<<===
"name": "Area 2", # this could essentially be removed and the dictionary key be used for the name
To access the tag: New Folder/Area 2/Sub Area 2/Tag 1.tagType
Now:
json['tags'][0]['tags'][0]['tags'][0]['tagType']
Mine:
json['tags']['Area 2']['tags']['Sub Area 2']['tags']['Tag 1']['tagType']
Consider that now, you don’t know the indexes of the tags arrays of the folders/tag you’re looking for, and so really every level you have to loop through each item in the tags array, read its name
field and compare it to what you’re looking for, then keep looping inside of that until you eventually get to what you want.
Using dictionaries would significantly reduce the complexity of code as well as increase the speed of execution, especially when you have 350k+ tags, or much larger systems with millions of tags.
The additional benefit would be that now tag json would be able to be compared using standard json compare tools. Currently this is impossible and custom compare scripts must be written so that like tag paths are compared and not just items of arrays whose order isn’t guaranteed to match (read: rarely matches). I actually convert these raw json arrays into dictionaries already whenever I need to do any comparisons but it’s far from efficient and speed is hampered.
Really, this applies to Views as well as its components all have names that could be used as the dictionary keys and would provide the same benefits.
Am I overlooking something?