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:
Working with the JSON DOM
Cancel

Working with JSON

Lesson 4: Parsing JSON into Structs

  1. Structs and arrays are commonly used to organize data in DataFlex. A structs structure is similar to JSON. Structs have members that are basically named value pairs. Structs can refer to other members, which match with nested objects. Structs can also have arrays of data. 
  2. Due to the similar structures of JSON and structs it makes sense to convert JSON into structs and arrays when working with them in DataFlex. The cJson object has an API for doing that. 
  3. This is an example of JSON with a matching struct definition. First, there is a tStudent struct that has a name, which matches the outermost object in the JSON that also has a name. Next in the struct, is a reference to tStudentDetail that pairs with the details object in the JSON, which has an age and a Boolean of “male.” Lastly in the struct, there is a number array of ratings that matches the ratings array in the JSON.
  4. Once there is a matching struct definition, conversions between the JSON and the struct can be made. This is done using the JSON to data type function, which is called after the JSON is parsed to fill a struct with the data or vice versa.

DEMONSTRATION – How to generate JSON

  1. After creating the detail object an array ratings object. hoJson, the outermost object, and hoDetail, with a defined age and a true Boolean, have been created. Next the ratings array is created. hoRatings is created and it is initialized as a jsonTypeArray. With a jsonTypeArray the AddMemberValue can be called to add members to the end of the array extending its size with each item.  Then the hoRatings is set as “rates” for the outermost object, hoJson. Finally, hoRatings is destroyed.
  2. Running the sample produces an output window that shows the name, details, and the array of numbers.

DEMONSTRATION – Converting JSON into a Struct

  1. At the beginning of the sample a struct called tStudent is being defined. It has a name, details and a rating. Details here refers to the tStudentDetail struct, which contains age and male.
  2. A JSON object is created that calls ParseString. The JsonToDataType is called to convert the JSON in memory into a struct called student (tStudent). The struct is used directly to fill the values. Finally, hoJson is destroyed.
  3. Running the sample produces an output window that shows “Student “John” has 4 ratings.” The number of ratings were counted.
  4. There are times when the JSON does not completely match the struct. For example, if the age is removed from the code an unhandled program error will be thrown.
  5. There is support for such an instance in the JSON parser. A property can be set that will allow for not all the members to be sent. Set pbRequireAllMembers of hoJson to False is added before the JsonToDataType. Running the program again in debug mode shows that the age has just been filled with a 0 because DataFlex does not support null.

DEMONSTRATION – Converting a Struct into JSON

  1. A sample procedure called StudentSTructToJson opens with a variable called “student” being created that is based on the tStudent type. The variables, such as name, age and details are moved, and items are added to the array.  
  2. A JSON object is then created, and then DataTypeToJson is called on it. The JSON structure is now created in memory. Next Stringify needs to be used to get it into a string.
  3. Running the sample produces an output window that shows the name, details, and the array of numbers.
  4. Occasionally, names used in the code do not align with the names used in the JSON. If this is necessary a meta tag can be added to create the connection.