Can you assign global variable to JSONSetElement

JSONSetElement [$data; $$MyJSon, JSONString]

$$MyJSon -> is a result from ExecuteSQL and formatted it as JSON

when assigning it gives no error, it gives me an error on runtime:

image

i tried to copy/paste the result from data viewer, and it works fine
image

Below is the alert message from my $$MyJSon
image

below is when i put a GLobal Variable (&&MyJSon)
image

Not sure what the error is because I don't see any. If you expected the result to be identical to the pasted result, then you should know that JSONSetElement escapes some unescaped values when setting JSONString elements. One that will always be escaped is the escape sequence character (\). The resulting JSON will always be a correct string and JSONString values must always reproduce the same string as was input when using JSONGetElement.

1 Like

ok, so you mean, JSONString element reProcess my JSON file thats why it has more \\ and \r ?

@marke1415 I have a suspicion that you might be trying to achieve something that would happen if you were specifying a different data-type, e.g., instead of JSONString, using JSONArray.

2 Likes

Close!

JSONSetElement processes value dependent on type. JSONString stores a string but the JSON itself is also a string. The value must be processed so that a call to JSONGetElement for keyOrIndexOrPath outputs the same value as was input by the call to JSONSetElement.

Try seeing what you get when you call JSONGetElement for the JSON you copied and pasted in your original post. It should become very obvious why JSONSetElement processes value.

1 Like

Your 'data' is not a string, but is an array, so your JSONSetElement should look something like:

JSONSetElement( "" ;
.......
[ "bordersize" ; "1.2" ; JsonString ] ;
[ "data" ; "[["Website","Source...........Close / Decline",16]]" ; JsonArray ] )

or

[ "data" ; "[" & $$MyJson & "]" ; JsonArray ] )

2 Likes

I'm not sure it is clear what you are trying to do. It looks like you have a syntax error in your first example:

JSONSetElement [$data; $$MyJSon, JSONString]

The JSONSetElement commands takes 4 parameters, using parentheses, and your example shows 3 in square brackets.
Here's the base usage of this function:
JSONSetElement ( json ; keyOrIndexOrPath ; value ; type )
That 1st json param is if you want to ADD the keyOrIndexOrPath/value key-value pair INTO some existing JSON.
Then, you say that you received an "alert message", but the images you included doesn't seem to actually include an error message, just a picture of some strings of info that look like nested JSON arrays.

Are you trying to take that 2-dimensional JSON array stored in $$MyJSon and put that INTO an existing JSON object stored in a variable named $data? That is definitely possible.
Here's an example. If the variable $startingJSON contains a simple JSON object like this:

{ "someKey1" : "someValue1" }

And, you want it to change so that there is a 2nd key inside named "data" that contains the nested JSON array you have stored in $$MyJSon, you would do this:

JSONSetElement ( $startingJSON
   ; [ "data" ; $$MyJSon ; JSONArray ]
  )

But, I'm not sure I understood correctly if that is what you are trying to do. More info would help.

1 Like