Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending 2 or more json string

I have form where in I dynamically create json strings and append those strings in a hidden field.

for example : Imagine I have a hidden field with id as "json_hidden_field" and my dynamically created jsons will look something like below

var json_str1 = "{ "name" : "foo1" , "value" : "bar1" }"

var json_str2 = "{ "name" : "foo2" , "value" : "bar2" }"

What I am looking for is to append the string in such a way that the resulting output is

"{ "name" : "foo1" , "value" : "bar1" } , { "name" : "foo2" , "value" : "bar2" }"

And to achieve this , this is what I have written but I am kind of annoyed by the fact that I have to handle that comma in such a way

  full_json= $("#json_hidden_field").val().concat($("#json_hidden_field").val() ? "," : "" , json_str2 )


$("#json_hidden_field").val(full_json)

I need help refactoring this piece of code.

Thanks

like image 881
Raghu Avatar asked Jul 18 '26 13:07

Raghu


1 Answers

var json_str1 = '{ "name" : "foo1" , "value" : "bar1" }'
var json_str2 = '{ "name" : "foo2" , "value" : "bar2" }'

full_json = [json_str1, json_str2].join(',');
like image 133
net.uk.sweet Avatar answered Jul 20 '26 03:07

net.uk.sweet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!