To continue with this content, please log in with your Data Access ID or create a new account.
Cancel Data Access ID
You may not be authorized to see this content. Please contact Data Access Europe for more information.
Cancel Data Access Europe
You are not authorized to see this content.
Cancel Data Access Europe
Next lesson:
Parsing JSON into structs
Cancel

Working with JSON

Lesson 3: Generating JSON

  1. The next step after parsing JSON is to generate JSON. Another sample, GenerateExample.src, will be used during this lesson.
  2. The JSON objects will be dynamically created, and then Stringify will be called at the end to get the string.
  3. First the JSON object, hoJson, is created using Get Create. Next the InitializeJsonType function is called, which initializes a JSON object as a certain type. When first created the object is not initialized, so it is not yet defined as an object, string, array, etc. From InitializeJsonType it is passed to type as a constant. In this sample jsonTypeObject is used to define it as an object. 
  4. Following, a member value is applied to it with the name, “name,” and it will be a string with a value of “John.” The value is passed a parameter.
  5. Another JSON object is then created, the hoDetail object. Two member values are applied: “age” as an integer with a value of 31, and “male” which a Boolean set to True. This object is then set as a member of the outermost object, hoJson. The name “details” is passed as hoDetail as the JSON object that is going to be the member of the outermost object. 
  6. hoDetail is then destroyed because it is no longer needed, but it does not destroy the JSON structure that was created in the background. It only destroys the handle to the object.
  7. peWhiteSpace is set (more to follow).
  8. Get Stringify is used to get the JSON string to generate the JSON for the objects that have been created.
  9. Showln sJson shows the string containing the JSON. It contains the name and details (age:31 and male:true). That JSON is put in an output window, and then the last object, hoJson, is destroyed.
    Running the sample results in an output window displaying the proper JSON.

Generating JSON – peWhiteSpace

  1. peWhiteSpace determines the format of the JSON being generated. 
  2. The standard value is jpWhitespace_Plain, which does not include any white space because machines do not need white space to recognize JSON. 
  3. There are two other forms of peWhiteSpace.
  4. jpWhitespace_Spaced, which adds spaces between the values to make it more readable. 
  5. upWhitespace_Pretty, which adds line views and indents the JSON. This is ideal when debugging or need to manually assess at a later time.