ideas for 2 new json builtins

+3 votes
asked Jan 28, 2022 in Wanted features by Martin (8,360 points)

While answering https://forum.plantuml.net/15326/add-edges-in-diagram-without-touching-existing-json-data, I thought about two new preprocessor built-in functions that would have been useful to make the idea more readable:

1) %get_json_type($json) returns int

  • (0 - Not valid Json, optional)
  • 1 - JSON array ($json starts '[')
  • 2 - JSON object ($json starts '{')
  • 3 - JSON other = string / number / boolean / null  (or give each its own return code)

2) %get_json_keys($json_object) returns new json array containing the keys from the parameter object.  For example '{"name": "Mark McGwire", "hr":   65, "avg":  0.278}' would return '["name","hr","avg"]'.  

These two combined would let you write:

!if %get_json_type($myjson) == JSON_OBJECT
   !foreach $key in %get_json_keys($myjson)
      !if %get_json_type($myjson[$key]) == JSON_STRING
         rectangle $myjson[$key]
      !endif
   !endfor
!endif

Just an idea.

1 Answer

+1 vote
answered Jan 29, 2022 by plantuml (295,000 points)
selected Jan 29, 2022 by Martin
 
Best answer

This is a brillant idea, we like it! Many thanks for the suggestion.

So with last snapshot (and on the online server), you can now have this:

This has not been really tested, so feedback is welcome :-)

commented Jan 30, 2022 by Martin (8,360 points)

Thank you.  Looks good!

I like the return values of strings rather than numbers, good idea.  Although maybe null should map to "null" rather than to "json"?

I notice that %get_json_keys not only works on a json object, but also works on a json array where all the elements are objects, I'm not sure how useful this is but it is neat!

commented Feb 21, 2022 by pprados (100 points)
Cool, but How it's possible to check if a key exist ?
commented Feb 21, 2022 by The-Lu (64,960 points)

Hello P., and all,

See similar request here:

With an attempts to `json_key_exists` on pure plantUML...
Regards.
...