issue with if else stmt

112 views
Skip to first unread message

Ashish Abrol

unread,
Aug 30, 2019, 12:26:45 PM8/30/19
to Jsonnet
Hi 
I have recently started with jsonnet and having trouble working with if else construct.
here is my sample program

*******************************************************
local var=true;
[
if var then {a:1},{d:3} else {a:2},
{c:2}

]

******************************************************
it results in error 
STATIC ERROR: one.jsonnet:4:26-29: Expected a comma before next array element.

I am expecting it to generate the following outcome

[
{a:1},
{d:3}},
{c:2}
]

can someone help me get this result.

thanks
Ashish

Dave Cunningham

unread,
Aug 30, 2019, 12:42:45 PM8/30/19
to Ashish Abrol, Jsonnet
The branches need to return arrays, then you add them on to the rest of your array:

local var=true;
(if var then [{a:1},{d:3}] else [{a:2}])
+ [{c:2}]


--
You received this message because you are subscribed to the Google Groups "Jsonnet" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsonnet+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jsonnet/aa4c098a-f632-4166-8028-602ed937e75a%40googlegroups.com.

Ashish Abrol

unread,
Aug 30, 2019, 1:14:40 PM8/30/19
to Jsonnet
I see that you have taken out the outer array brackets [] from the program. Although it works but it becomes a nested array structure now. Which i want to avoid?



On Friday, 30 August 2019 22:12:45 UTC+5:30, Dave Cunningham wrote:
The branches need to return arrays, then you add them on to the rest of your array:

local var=true;
(if var then [{a:1},{d:3}] else [{a:2}])
+ [{c:2}]


On Fri, Aug 30, 2019 at 5:26 PM Ashish Abrol <ashis...@gmail.com> wrote:
Hi 
I have recently started with jsonnet and having trouble working with if else construct.
here is my sample program

*******************************************************
local var=true;
[
if var then {a:1},{d:3} else {a:2},
{c:2}

]

******************************************************
it results in error 
STATIC ERROR: one.jsonnet:4:26-29: Expected a comma before next array element.

I am expecting it to generate the following outcome

[
{a:1},
{d:3}},
{c:2}
]

can someone help me get this result.

thanks
Ashish

--
You received this message because you are subscribed to the Google Groups "Jsonnet" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jso...@googlegroups.com.

Ashish Abrol

unread,
Aug 30, 2019, 1:28:28 PM8/30/19
to Jsonnet
thanks Dave!
i figured it out for my use case.

Cheers!
Ashish
Reply all
Reply to author
Forward
0 new messages