why %json_key_exists doesn't protect from missing keys?

0 votes
asked Oct 1, 2025 in Question / help by Kuba

I want to handle a case when a given key is not defined in a json object.

For some reason this works:
 

!if %json_key_exists($object, $key)

participant yes

!else

participant no

!endif

but this doesn't, and ends with "Data parsing error":

!if %json_key_exists($object, $key)

!if $object[$key]=="charm"

participant charm

!else

participant strange

!endif

!else

participant no

!endif

A more complete example:

https://www.plantuml.com/plantuml/uml/dP71Qe0m38RlVOeO72Xusd4W-1vZI6eZTDDMsWgJiNTVJOSNns3BnJ-_Ny8V7MAYaFPrKQgg3IM27ZuKv4B29y3PU-nAhyzUKq3rcU4j-8dD7XXgCgQf7QtSgihEyF0QlHlV-1ZvtSOK6zdRGK5arqOXsSbkv5AnWM8Hgk8bia3dbAR8Ty8PEylZ2d4BzFZ7H2ScxhEOBtqlfykxtnCBSgMMqdvtXiBn8odf_tscV6maRB_Lb9Kg1wCMLwJ9SiYJ-ST-0G00

I feel I'm missing something obvious. I wasn't able to find an answer though.

3 Answers

0 votes
answered Oct 1, 2025 by The-Lu (87,240 points)

Hello K.,

  • What is your version?

But your second example works:

!if %json_key_exists($object, $key)

!if $object[$key]=="charm"

participant charm

!else

participant strange

!endif

!else

participant no

!endif

Regards,
Th.

commented Oct 1, 2025 by anonymous
I am using 1.2025.4

When I test the example (link in my original post) I'm seeing the same error as locally:

https://drive.google.com/file/d/1GzNmLLDx2bmzoUL15izE7zYuNDNIuw-w/view?usp=drive_link

(sorry, I don't know how to include image in the post).
 

As far as I can see the server uses 1.2025.8beta1
0 votes
answered Oct 1, 2025 by The-Lu (87,240 points)

Hello K.

For that:

  • Could you use a temporary variable?
@startuml

!$data = {
    "a": "foo",
    "b": "bar"
}

!procedure $add2($name)
!if %json_key_exists($data, $name)
    !$t=$data[$name]
    !if $t=="foo"
        participant $name as $name
    !else
        boundary $name as $name
    !endif
!else
    database $name as $name
!endif
!endprocedure

$add2("a")
$add2("b")
$add2("c")
@enduml

Enjoy,

Regards,
Th.

commented Oct 2, 2025 by anonymous
Thank you very much, this works indeed!

So the remaining question is - why?

Is it a bug in plantuml, or is there some rule I missed, or some technical detail of the execution model that requires this trick?
I'm asking to be able to solve the issue myself in the future.
commented Oct 3, 2025 by The-Lu (87,240 points)

Hi A.,

Thanks for your report.

Yes there is currently a bug, and that is a workaround.
And FYI the preprocessing is not a full complete preprocessing... 

The reason why we had open this issue:

Thanks,
Regards,
Th.

0 votes
answered Jan 22 by The-Lu (87,240 points)

Thank K.for the report.

FYI, that is now fixed from V1.2026.0:

Regards,
Th.
...