# Can you assign global variable to JSONSetElement

**URL:** <https://the.fmsoup.org/t/can-you-assign-global-variable-to-jsonsetelement/3633>\
**Category:** Questions\
**Tags:** filemaker\
**Created:** [July 12, 2023, 11:53am UTC](https://the.fmsoup.org/t/can-you-assign-global-variable-to-jsonsetelement/3633 "2023-07-12T11:53:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![marke1415](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@marke1415](https://the.fmsoup.org/u/marke1415)\
**Post date:** [July 12, 2023, 11:53am UTC](https://the.fmsoup.org/t/can-you-assign-global-variable-to-jsonsetelement/3633/1 "2023-07-12T11:53:50Z")

</div>

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](https://canada1.discourse-cdn.com/flex030/uploads/fmsoup/original/2X/5/56796db82313359db4fd72ec5e11b4f9cee0c408.png)

**i tried to copy/paste the result from data viewer, and it works fine**  
 ![image](https://canada1.discourse-cdn.com/flex030/uploads/fmsoup/original/2X/d/df9404563ec7ebd941e53e0c887bf19e29874e05.png)

**Below is the alert message from my $$MyJSon**  
 ![image](https://canada1.discourse-cdn.com/flex030/uploads/fmsoup/original/2X/e/eca9fbf5ee37f72437e7dcd9dfcbb8de7c5a9634.png)

**below is when i put a GLobal Variable (&&MyJSon)**  
 ![image](https://canada1.discourse-cdn.com/flex030/uploads/fmsoup/original/2X/5/557bd0a14ebfe9ae1304bbde15f4fe824aca478f.png)

---

<div class="post-metadata">

**Author:** ![bdbd](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/bdbd/32/620_2.png) [@bdbd](https://the.fmsoup.org/u/bdbd)\
**Post date:** [July 12, 2023, 2:21pm UTC](https://the.fmsoup.org/t/can-you-assign-global-variable-to-jsonsetelement/3633/2 "2023-07-12T14:21:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![marke1415](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@marke1415](https://the.fmsoup.org/u/marke1415)\
**Post date:** [July 12, 2023, 3:39pm UTC](https://the.fmsoup.org/t/can-you-assign-global-variable-to-jsonsetelement/3633/3 "2023-07-12T15:39:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![steve\_ssh](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/steve_ssh/32/160_2.png) [@steve\_ssh](https://the.fmsoup.org/u/steve_ssh)\
**Post date:** [July 12, 2023, 4:59pm UTC](https://the.fmsoup.org/t/can-you-assign-global-variable-to-jsonsetelement/3633/4 "2023-07-12T16:59:30Z")

</div>

> [@marke1415](#):
>
> JSONSetElement [$data; $$MyJSon, JSONString]

@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.

---

<div class="post-metadata">

**Author:** ![bdbd](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/bdbd/32/620_2.png) [@bdbd](https://the.fmsoup.org/u/bdbd)\
**Post date:** [July 12, 2023, 10:57pm UTC](https://the.fmsoup.org/t/can-you-assign-global-variable-to-jsonsetelement/3633/5 "2023-07-12T22:57:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![menno](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/menno/32/2016_2.png) [@menno](https://the.fmsoup.org/u/menno)\
**Post date:** [July 16, 2023, 8:34pm UTC](https://the.fmsoup.org/t/can-you-assign-global-variable-to-jsonsetelement/3633/6 "2023-07-16T20:34:07Z")

</div>

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** ] )

---

<div class="post-metadata">

**Author:** ![DanShockley](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@DanShockley](https://the.fmsoup.org/u/DanShockley)\
**Post date:** [July 20, 2023, 6:24pm UTC](https://the.fmsoup.org/t/can-you-assign-global-variable-to-jsonsetelement/3633/7 "2023-07-20T18:24:01Z")

</div>

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:

```auto
{ "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:

```auto
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.
