JSON: Difference between revisions
Format and types
m (Add link to Starzen library) |
(Format and types) |
||
Line 2: | Line 2: | ||
It is a format that is often used in RESTful webservices and as such it is important to be able to use it from within DataFlex. | It is a format that is often used in RESTful webservices and as such it is important to be able to use it from within DataFlex. | ||
==JSON Format== | |||
JSON is a very simple format, derived from [[JavaScript]]'s [https://www.dyn-web.com/tutorials/object-literal/ Object Literal notation], consisting of a series of name/value pairs. | |||
The names are quoted with <u>double-quote</u> characters: <font color="blue">"''name''"</font>. | |||
How values are written depends on the data-type (see below). | |||
Names are separated from values by colons: <font color="blue">"name":''value''</font>. | |||
The pairs are separated from each other with commas: <font color="blue">"name1":''value1'', "name2":''value2'', "name3":''value3''...</font> (the last pair in such a series should <u>not</u> be followed by a comma however). | |||
Special character may be "escaped" in string values (or indeed names) with a backslash: <font color="blue">'''\'''</font> | |||
*'''\\''' represents '''<font color="blue">\'''</font> (backslash) | |||
*'''\/''' represents '''<font color="blue">/'''</font> (forward slash) | |||
*'''\"''' represents '''<font color="blue">"'''</font> (double-quote) | |||
*'''\b''' represents backspace (ASCII 8) | |||
*'''\f''' represents formfeed (ASCII 12) | |||
*'''\n''' represents newline (ASCII 10) | |||
*'''\r''' represents carriage-return (ASCII 13) | |||
*'''\t''' represents tab (ASCII 9) | |||
[https://en.wikipedia.org/wiki/Unicode Unicode] characters (up to FFFF: 65,535, which covers the [https://en.wikipedia.org/wiki/Plane_(Unicode)#Basic_Multilingual_Plane basic multilingual plane]) may be represented by '''\u''HHHH''''', where "'''''H'''''" is a hexadecimal-digit (0-F). | |||
==JSON Data Types== | |||
JSON values can be one of six data-types - four "primitive" and two "compound": | |||
'''Primitive''': | |||
*'''Strings''', which, like names, must be quoted with <u>double-quote</u> characters: <font color="blue">"value"</font> | |||
*'''Numbers''', which can be simple integers, or decimal values, or exponentiated (using either "e" or "E") and may be negative: | |||
**<font color="blue">6</font> | |||
**<font color="blue">13429064</font> | |||
**<font color="blue">-9645</font> | |||
**<font color="blue">23.657685</font> | |||
**<font color="blue">-29.41</font> | |||
**<font color="blue">1.23456e7</font> (indicating 12,345,600) | |||
**<font color="blue">-456.789E5</font> (indicating -45,678,900) | |||
**<font color="blue">6.281e-6</font> (indicating 0.000006281) | |||
*'''Boolean''', which can have the value of either <font color="blue">true</font> or <font color="blue">false</font> (unquoted) | |||
*'''Null''', which is simply represented by <font color="blue">null</font> (unquoted) | |||
'''Compound''': | |||
*'''Objects''', which are enclosed in '''{''' ... '''}''' characters and generally contain additional name/value pairs: <font color="blue">{"surname":"Peat", "forename":"Mike", "age":21, "is male":true, "salary":null}</font> | |||
*'''Arrays''', which are enclosed in '''[''' ... ''']''' characters and are made up of values separated by commas: <font color="blue">[5, "Mike", false, 14.70912, null, -5.34108e9]</font> | |||
Whitespace is irrelevant <u>outside</u> of quotations (although both names and string values may contain whitespace). Because of this, JSON may be formatted for easier human-readability (''prettified'') with spaces and line breaks: | |||
<font color="blue">{ | |||
"first name": "Mike", | |||
"last name": "Peat", | |||
"age": 21, | |||
"is male": true, | |||
"salary": null, | |||
"address": { | |||
"house": 22, | |||
"street": "Acacia Avenue", | |||
"town": "Dullsville", | |||
"county": "Midhamptonshire" | |||
}, | |||
"test scores": [56, 87, 19, 11, 70, 64] | |||
}</font> | |||
==JSON in DataFlex== | |||
Before DataFlex 19.0 you had to resort to external libraries in order to use JSON, see for example [http://support.dataaccess.com/Forums/showthread.php?54830-JSON-Parsing-the-beginnings-of-an-alternative-approach JSON Parsing ... the beginnings of an alternative approach] | Before DataFlex 19.0 you had to resort to external libraries in order to use JSON, see for example [http://support.dataaccess.com/Forums/showthread.php?54830-JSON-Parsing-the-beginnings-of-an-alternative-approach JSON Parsing ... the beginnings of an alternative approach] | ||
Line 13: | Line 78: | ||
When migrating data from JSON to a struct sometimes a member might be missing from the JSON data. For example because the element you are looking for is empty. In that case the runtime will trigger a runtime error. You can disable that by setting the [https://docs.dataaccess.com/dataflexhelp/mergedProjects/VDFClassRef/cJsonObject-Property-pbRequireAllMembers.htm pbRequireAllMembers] property of the DataFlex Json object to false. | When migrating data from JSON to a struct sometimes a member might be missing from the JSON data. For example because the element you are looking for is empty. In that case the runtime will trigger a runtime error. You can disable that by setting the [https://docs.dataaccess.com/dataflexhelp/mergedProjects/VDFClassRef/cJsonObject-Property-pbRequireAllMembers.htm pbRequireAllMembers] property of the DataFlex Json object to false. | ||
If you | If you need to deal with JSON which uses DataFlex reserved words (or other invalid values) in its member names then, since DataFlex 19.1, you can now use a valid name in your struct and assign a different name for the conversion via meta-data tags. This is sometimes referred to as the [https://docs.dataaccess.com/dataflexhelp/mergedProjects/Tools/Name_Meta-Data_Tag.htm altName member] | ||
== External references == | == External references == |