It's not possible to refer to JSON nodes as if they are predefined objects. Perhaps you could suggest a syntax for your "apple -> red" link. e.g. Are you thinking something like: json::fruits::name("apple") --> json::colors::name("red") ? Or were you hoping to automatically map all fruits colorid to their respective colors id?
Anyway, I had fun writing an alternative json visualiser in the object diagram syntax using the preprocessor. It is likely not very robust as I've only tried it on the one example json - it's just to give you an idea of what's possible.
(click for online server)
@startuml
!$myjson = {
"root" : [{
"fruits": [
{
"name": "apple",
"colorId": "1"
},
{
"name": "pear",
"colorId": "2"
},
{
"name": "pineapple",
"colorId": "3"
}
]
},
{
"colors": [
{
"id": "1",
"name": "red"
},
{
"id": "2",
"name": "green"
},
{
"id": "3",
"name": "yellow"
}
]
}]
}
set namespaceSeparator none
!$i = 1
!procedure $debug($text1, $text2)
object %string($i + "_" + $text1) {
%string($text2)
%size($text2)
}
!$i = $i +1
!endprocedure
!procedure process_json($stem, $json)
!if %substr($json,0,1) != "{"
map %string($stem) {
}
!endif
!if %substr($json,0,1) == "["
'!if $i > 1
' $debug("square", $json)
'!endif
!$j=0
!foreach $node in $json
'$debug("element", $node)
process_json(%string($stem+"."+$j), $node)
$stem --> %string($stem+"."+$j)
!$j=$j+1
!endfor
!elseif %substr($json,0,1) == "{"
'$debug("curly", $json)
' parse for map
map $stem {
!$rest=$json
!while $rest != "{"
!$key = %substr($rest,2,%strpos($rest, ":")-3)
!$value = $json[$key]
!if %substr($value,0,1) != "["
$key => $value
!endif
!$rest = "{" + %substr($rest,%strlen("{"+%chr(34)+$key+%chr(34)+":"+%chr(34)+$json[$key]+%chr(34)+"}"))
!endwhile
}
' parse again for recursions
!$rest=$json
!while $rest != "{"
!$key = %substr($rest,2,%strpos($rest, ":")-3)
!$value = $json[$key]
!if %substr($value,0,1) == "["
'$debug("key", $key)
'$debug("value", $value)
process_json(%string($stem+"."+$key), $value)
$stem --> %string($stem+"."+$key)
!endif
!$rest = "{" + %substr($rest,%strlen("{"+%chr(34)+$key+%chr(34)+":"+%chr(34)+$value+%chr(34)+"}"))
'$debug("rest", $rest)
!endwhile
!else
'$debug("string", $json)
!endif
!endprocedure
process_json("root", $myjson.root)
root.0.fruits.0::apple -> root.1.colors.0::red
@enduml