 |
oldguy2 Wizard
Joined: 17 Jun 2006 Posts: 1201
|
Posted: Fri Jun 18, 2010 7:43 pm
JSON Commands? |
Is there a post somewhere explaining the JSON syntax we are supposed to use or is this all internal and hidden to us so we don't need to worry about it? I really haven't see anything explaining it, or at least cannot find the post that did through the maze of posts here. 
|
|
|
 |
Zugg MASTER

Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Fri Jun 18, 2010 8:38 pm |
JSON stands for "JavaScript Object Notation" and is a standard data format used on the Internet by many different data services (such as Amazon, Google, etc). It is documented at json.org
CMUD supports both the strict JSON syntax as well as a "relaxed" syntax. For example, when specifying a json object with a key of "key1" and a value of "value1", the strict syntax is:
Code: |
{"key1":"value1", "key2":"value2"} |
but CMUD supports removing the quotes if the strings do not contain any spaces or special characters. So CMUD supports this:
Code: |
{key1:value1, key2:value2} |
You'll not that JSON is very similar to the normal CMUD database format where CMUD uses "=" and "|" while JSON uses ":" and ",". This is why it was possible to adapt the json parser to handle normal CMUD string lists and database variables.
Normally you should NEVER worry about JSON syntax in CMUD. CMUD will show the internal JSON structure in the output of the #VAR command and in the XML export output. I might eventually add a JSON tab to the Variable Editor for debugging purposes.
But except for using the %json function to take a json string and convert it into a table variable, you should never need to use the JSON syntax for anything in CMUD.
Scripts should use commands and functions like #ADDITEM, #ADDKEY, %additem, %addkey etc to build lists and tables. The backward compatible syntax of {a|b|c..} for string lists and {key1=value1|key2=value2...} can still be used for simple lists and tables. If you find yourself needing to know more details about the internal structure, then you are probably doing something the wrong way. |
|
|
 |
oldguy2 Wizard
Joined: 17 Jun 2006 Posts: 1201
|
Posted: Fri Jun 18, 2010 8:51 pm |
No basically I was just wondering what commands you added to use with it. So apparently there is a %json function. Is there an example of how to use this function somewhere?
|
|
|
 |
GeneralStonewall Magician
Joined: 02 Feb 2004 Posts: 364 Location: USA
|
Posted: Fri Jun 18, 2010 8:53 pm |
Just %json and all it does is return the JSON value.
#print {%json( @var)} |
|
|
 |
Zugg MASTER

Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Fri Jun 18, 2010 9:00 pm |
No documentation until closer to public version.
The %json function is the only command/function that does anything with JSON. It can be used in two ways: to create a table from a json string, or to return the json string from an existing table.
1) Creating a table from a json string:
Code: |
jsonstring = "{key1:value1, key2:value2}"
table = %json( @jsonstring) |
If you do a "#VAR table" you will see that the @table variable has been created with the table structure specified by the json string. This function can be useful if you obtain a json string from another data source, such as an Amazon web services query and you want to create a CMUD list/table from it.
2) Retrieve the json string of an existing table:
Code: |
list=""
#ADDITEM list 10
#ADDITEM list 20
#ADDITEM list "Zugg"
#SHOW %json( @list) |
this will display:
[10,20,"Zugg"]
which is the json string syntax for the @list table that was created. I use this function extensively in my test scripts to determine if the underlying json table structure of a variable is correct because the json string value can determine the difference between things that the normal CMUD string list format cannot handle.
For example, consider the following list:
Code: |
List=""
#ADDITEM List 10
#ADDITEM List "20"
#ADDITEM List "Zugg" |
The CMUD string list value of @list is just:
10|20|Zugg
which cannot tell the difference between "20" as a string literal value vs 20 the number. If you look at the %json( list) result, you would see:
[10,"20","Zugg"]
which properly shows that "20" is being stored as a string value rather than a number like 10.
The ability of json syntax to distinguish between these kind of details is what makes it able to properly handle nested lists and database variables which the old CMUD code could not handle correctly. |
|
|
 |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|