json update boolean value

49 views
Skip to first unread message

D Panda

unread,
May 2, 2022, 8:47:21 AM5/2/22
to Jansson users
Hi all,

I'm using Jansson to read and work on a Kubernetes configuration file. I have a line in the json file as follows :Screenshot from 2022-05-02 18-13-19.png
I read it in my C program as this:
Screenshot from 2022-05-02 18-14-29.png

As expected, this gives me a boolean. But I need to change this value to "false". I found the methods for setting strings, integers, and other values but not for boolean. Please let me know how to update the value for a boolean using Jansson.

Thanks!

D Panda

unread,
May 2, 2022, 8:52:30 AM5/2/22
to Jansson users
I also considered deleting the key/value from the json object and adding it back in with the new value, but I cannot seem to set the key for boolean values.

Graeme Smecher

unread,
May 2, 2022, 6:47:53 PM5/2/22
to jansso...@googlegroups.com
Hi D Panda,

If I understand your question correctly, something like:

json_object_set(K8S_TEMPLATE_SECURITY_CONTEXT,
"allowPrivilegeEscalation",
json_false());

or

json_object_set(K8S_TEMPLATE_SECURITY_CONTEXT,
"allowPrivilegeEscalation",
json_bool(0));

...should do what you want.

If a survey of the API is useful, then Jansson's test suites might be a
helpful resource. There are a few examples of the boolean API in
"test_simple.c" and "test_fixed_size.c" [1]. This code is intended to
test Jansson comprehensively (at the expense of being idiomatic).

If I am misunderstanding your question, please feel free to explain in
more detail (and please include code! It's always easier to help when
you provide source code.)

best,
Graeme

[1]:
https://github.com/akheron/jansson/blob/master/test/suites/api/test_fixed_size.c#L45

On 2022-05-02 05:52, 'D Panda' via Jansson users wrote:
> I also considered deleting the key/value from the json object and adding
> it back in with the new value, but I cannot seem to set the key for
> boolean values.
>
> On Monday, 2 May 2022 at 18:17:21 UTC+5:30 D Panda wrote:
>
> Hi all,
>
> I'm using Jansson to read and work on a Kubernetes configuration
> file. I have a line in the json file as follows :Screenshot from
> 2022-05-02 18-13-19.png
> I read it in my C program as this:
> Screenshot from 2022-05-02 18-14-29.png
>
> As expected, this gives me a boolean. But I need to change this
> value to "false". I found the methods for setting strings, integers,
> and other values but not for boolean. Please let me know how to
> update the value for a boolean using Jansson.
>
> Thanks!
>
> --
> --
> Jansson users mailing list
> jansso...@googlegroups.com
> http://groups.google.com/group/jansson-users
> <http://groups.google.com/group/jansson-users>
> ---
> You received this message because you are subscribed to the Google
> Groups "Jansson users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to jansson-user...@googlegroups.com
> <mailto:jansson-user...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jansson-users/c8c86ea4-39c3-4eb2-bc9b-03413790fe61n%40googlegroups.com
> <https://groups.google.com/d/msgid/jansson-users/c8c86ea4-39c3-4eb2-bc9b-03413790fe61n%40googlegroups.com?utm_medium=email&utm_source=footer>.

D Panda

unread,
May 5, 2022, 11:07:56 AM5/5/22
to Jansson users
Thank you, Graeme  . This is precisely what I needed. My program now works as expected.
Reply all
Reply to author
Forward
0 new messages