JSON: Difference between revisions

1,113 bytes added ,  17 June 2021
(Format and types)
 
(6 intermediate revisions by one other user not shown)
Line 5: Line 5:
==JSON Format==
==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.
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 [https://en.wikipedia.org/wiki/Attribute%E2%80%93value_pair name/value pairs] with arbitrarily deep nesting.


The names are quoted with <u>double-quote</u> characters: <font color="blue">"''name''"</font>.
The names are quoted with <u>double-quote</u> characters: <font color="blue">"''name''"</font>.
Line 15: Line 15:
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).
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>
Special characters 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> (backslash)
*'''\/''' represents '''<font color="blue">/'''</font> (forward slash)
*'''\/''' represents '''<font color="blue">/'''</font> (forward slash)
Line 31: Line 31:
JSON values can be one of six data-types - four "primitive" and two "compound":
JSON values can be one of six data-types - four "primitive" and two "compound":


'''Primitive''':
====Primitive:====
*'''Strings''', which, like names, must be quoted with <u>double-quote</u> characters: <font color="blue">"value"</font>
*'''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:
*'''Numbers''', which can be simple integers, or decimal values, or exponentiated (using either "e" or "E") and may be negative:
Line 45: Line 45:
*'''Null''', which is simply represented by <font color="blue">null</font> (unquoted)
*'''Null''', which is simply represented by <font color="blue">null</font> (unquoted)


'''Compound''':
====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>
*'''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>
*'''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:
==Prettifying==
Whitespace is irrelevant <u>outside</u> of double-quotes (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, so:
 
<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>.
 
Is the same, from a machine standpoint, as the rather more human-readable:


  <font color="blue">{
  <font color="blue">{
Line 65: Line 70:
     "test scores": [56, 87, 19, 11, 70, 64]
     "test scores": [56, 87, 19, 11, 70, 64]
  }</font>
  }</font>
Which is only slightly less compact "on the wire" (by around 25 bytes).


==JSON in DataFlex==
==JSON in DataFlex==
Line 73: Line 80:
This functionality is offered via the [https://docs.dataaccess.com/dataflexhelp/mergedProjects/VDFClassRef/cJsonObject.htm cJsonObject]
This functionality is offered via the [https://docs.dataaccess.com/dataflexhelp/mergedProjects/VDFClassRef/cJsonObject.htm cJsonObject]


One of the great features in that class is that you can move all your data from JSON into a struct with just one command and vice versa.
One of the great features in that class is that you can move all your data from JSON into a struct with just one function or procedure call, and vice versa. In order to transfer your data from JSON to a struct you would use the [https://docs.dataaccess.com/dataflexhelp/mergedProjects/VDFClassRef/cJsonObject-Function-JsonToDataType.htm JsonToDataType] function and if you have to convert data from a struct to JSON then you can use the [https://docs.dataaccess.com/dataflexhelp/mergedProjects/VDFClassRef/cJsonObject-Procedure-DataTypeToJson.htm DataTypeToJson] procedure.
In order to transfer your data from JSON to a struct you would use the [https://docs.dataaccess.com/dataflexhelp/mergedProjects/VDFClassRef/cJsonObject-Function-JsonToDataType.htm JsonToDataType] function and if you have to convert data from a struct to JSON then you can use [https://docs.dataaccess.com/dataflexhelp/mergedProjects/VDFClassRef/cJsonObject-Procedure-DataTypeToJson.htm DataTypeToJson].


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, so it has simply been omitted from the JSON. 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 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]
If you need to deal with JSON which uses DataFlex reserved words in its member names (or other invalid values, such as those containing spaces in the example above: e.g. "first name") 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 ==
Line 87: Line 93:


* [http://starzen.com/products/utility-libraries/json-library/ Starzen JSON Library]
* [http://starzen.com/products/utility-libraries/json-library/ Starzen JSON Library]
*[https://support.dataaccess.com/Forums/showthread.php?65503-cJsonPath-class cJsonPath class] - Mike Peat, 6th Feb 2020. Easily get either objects or values from deep inside pretty complex JSON data.
*[https://support.dataaccess.com/Forums/showthread.php/65493-JsonConfig-pkg JsonConfig.pkg] - Mike Peat, 5th Feb 2020. A little singleton object-package which can read in a JSON configuration file and then allows you to read various settings out of it.


[[Category:REST]]
[[Category:REST]]
[[Category:JSON]]
[[Category:JSON]]
[[Category:System Integration]]
[[Category:System Integration]]