JSON functions: Get all keys, check if key exists in JSON, default value in get_variable_value

+1 vote
asked Feb 9, 2022 in Wanted features by tom10271 (140 points)
edited Feb 9, 2022 by tom10271

1 Answer

0 votes
answered Feb 9, 2022 by plantuml (294,960 points)

That's a good idea.

It would help if you could post here some short snippets that would show simple examples.

Something like:

@startuml
!$myjson = {
"name": "Mark McGwire", "hr": 65, "avg":  0.278
}

!foreach $key in %get_json_keys($myjson)
     rectangle $key
!endfor
@enduml

For function names, you can get inspired by other programming languages or library.

Thanks!

commented Feb 9, 2022 by tom10271 (140 points)
In PHP it has array_key_exists and array_keys

So:

%json_key_exists($data, "theKey")

%json_keys($data, "theKey")
commented Feb 9, 2022 by The-Lu (63,920 points)

Hello all,

Here is an implementation of `json_key_exists` on pure plantuml:

@startuml
!$myjson = {
  "name": "Mark McGwire", "hr": 65, "avg":  0.278
}

!function json_key_exists($myjson, $test)
  !foreach $key in %get_json_keys($myjson)
    !if ($key == $test)
      !return %true()
    !endif
  !endfor
  !return %false()
!end function

:json_key_exists($myjson, "hr");
:json_key_exists($myjson, "foo");
:json_key_exists($myjson, null);
@enduml

Beware of special key... 
See also:

Regards,
Th.

commented Feb 9, 2022 by tom10271 (140 points)
Is get_json_keys avaliable in any release?
commented Feb 9, 2022 by The-Lu (63,920 points)
commented Feb 9, 2022 by tom10271 (140 points)
Amazing. I was brothered for hours on figuring out how to check if key exists in PlantUML.

Thank you so much
...