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:
Handling large UTF 8 strings

Working with JSON

Lesson 5: Working with the JSON DOM

  1. When JSON is parsed or JSON objects have been created a Document Object Model (DOM) is in memory that can be stringified or it can be put into a struct. Functions can also be used to query values.
  2. Following is an example of how to use the DOM structure to go over a JSON structure. 
  3. Starting is a recursive function called IterateDOM that has a JSON object and a string passed to it. The string will contain spaces for indenting. 
  4. The output window will show the information that will be passed.
  5. Next the type of the object is retrieved. If it is jsonTypeObject then the members of the object will be gone over. If it is jsonTypeArray then the members of the array will be gone over. If it is a Boolean, double, integer or string the value will be outputted.

  6. After that, the information will be called and parsed into a JSON string creating the hoJson object, which contains the initialize JSON. Then the IterateDOM function is stepped into that has the outermost object as its parameter. The jsonTypeObject is OBJECT, so the members of the object will be looped over. MemberCount is used to get the number of members, then the member names are retrieved using Get MemberNameByIndex which gets the name of the member. The first member is called “name.” Get Member is used to get the value of “name.” The function is the recursively called by going into IterateDOM again. hoJson then points to the member of type string. The JsonValue function is used to get the value of the member. In this case, hoJson points to a string type. The member value will be outputted to the window. hoMember is then destroyed.  
  7. Going one level up in the recursive function, the next member is “details.” IterateDOM is called again. hoJson now points to the details object, which calls “age” and “male.”
  8. Going one level up in the recursive function, the next member is “ratings.” IterateDOM is called again. hoJson now points to an array, and it goes over the array members. Array members do not have names, so MemberByIndex is used to get the member by the index. A For loop is used that goes from zero to MemberCount - 1. For each member, InterateDOM is called. The members are of type DOUBLE. INTEGER will be used, however, if the number does not contain a decimal. When looped through again the next member in the array has a decimal, so it is of type DOUBLE.
  9. NOTE: jsonTypeInteger will be used if the member does not have decimals. If the member does have decimals it will use jsonTypeDouble. When creating JSON is does not matter if jsonTypeDouble is passed when decimals are not present. It will not add a decimal and a zero (.0). When parsing JSON, however, the number can be both jsonTypeDouble or jsonTypeInteger depending on whether or not it has decimals. 
  10. Running the sample results in an output window that shows all the details along with their types.
  11. The next sample, called Copy Data, will show how to change an object with DOM: a details object will be copied into another JSON structure.
  12. In the sample two JSON objects are created. The first one is hoJson2 that will be initialized as a JSON object, and the second is hoJson that will have a string parsed into it rather than initializing it. 
  13. Get Member is used to pull the details from hoJson. Details are then added to the second JSON object that was created, hoJson2.
  14. A member value is then changed. The “age” is changed to 54. Stringify is then used on hoJson2, and its value now shows the age changed to 54. Next, Stringify is used on hoJson, and it also now shows an age set to 54. This shows that it all works by reference. With Get Member a reference is retrieved for that member, and if the member is added to another JSON document or object a copy isn’t made in memory. A reference is just made to the other object. JSON objects can be part of multiple JSON documents. Changing one thing will change it in all locations. Both hoJson and hoJson2 need to be destroyed otherwise the details will remain in memory.
  15. If a copy of JSON needs to be made without that relation being made, use Stringify to get the JSON string and ParseString to parse the string to result in an identical object that is not related to the original object. 
  16. REVIEW
  17. There are functions for parsing JSON: ParseString and ParseUtf8. There are functions for generating JSON: Stringify and StringifyUtf8. There are functions for converting JSON to structs and arrays: DataTypeToJson and JsonToDataType. There are functions to travers JSON DOM when its in memory: Member, MemberByIndex, MemberCount, MemberNameByIndex, HasMember, JsonType. JSON DOM can also be manipulated with AddMember, SetMember, SetMemberValue, and InitializeJsonType.