DEV Community

zuUwn21
zuUwn21

Posted on

JSON-to-Struct with Uniface 10.4 jsonToStruct: A Practical Guide πŸ“„βš‘

What is jsonToStruct? πŸ€”
The jsonToStruct statement in Uniface is a ProcScript function that converts JSON text into a Uniface structure (Struct). This function is particularly valuable when receiving data from web APIs or processing JSON files.

Basic Syntax

jsonToStruct StructTarget, JsonSource
Enter fullscreen mode Exit fullscreen mode

Parameters:

  • StructTarget:
    Variable, parameter, or non-database field to hold the generated structure

  • JsonSource:
    JSON text as string, variable, field, or filename

Practical Example πŸ’»
Here's a simple example of how the function is used:

variables
    vStruct : struct
    vJsonText : string
endvariables

vJsonText = '{"name": "John Doe", "age": 30, "skills": ["C#", "Uniface", "SQL"]}'
jsonToStruct vStruct, vJsonText

; Accessing the data
putmess vStruct->name        ; Output: John Doe
putmess vStruct->age         ; Output: 30
putmess vStruct->skills->{1} ; Output: C#
Enter fullscreen mode Exit fullscreen mode

Understanding JSON Structures πŸ“Š
JSON can have two main forms:

JSON Objects
Use curly braces { } and contain key-value pairs:

{
  "name": "Developer",
  "experience": 5,
  "active": true
}
Enter fullscreen mode Exit fullscreen mode

Struct members are addressable by name: vStruct->name

JSON Arrays
Use square brackets [ ] and contain indexed values:

["JavaScript", "Python", "C#", "Uniface"]
Enter fullscreen mode Exit fullscreen mode

Array elements are addressable by index: vStruct->*{1} (first element)

Data Type Mappings πŸ”„
Uniface automatically recognizes JSON data types and maps them accordingly:

JSON Type Uniface Struct Annotation
string jsonDataType = "string"
number jsonDataType = "number"
boolean jsonDataType = "boolean"
null jsonDataType = "null"

Error Handling ⚠️
The function returns various status codes:

statuc code descritption
0 Conversion successful
-4 File could not be opened
-13 Filename too long
-1900 JSON string empty or invalid
-1901 JSON text doesn't start with { or [
-1902 Parser error
jsonToStruct vStruct, vJsonFile

if ($status < 0)
    putmess "Error parsing: " ; $procerror
else
    putmess "JSON successfully converted!"
endif
Enter fullscreen mode Exit fullscreen mode

Working with Files πŸ“
The function can also process JSON files directly:

jsonToStruct vStruct, "data.json"
Enter fullscreen mode Exit fullscreen mode

Important Note: The file must be Unicode-encoded. If not, use fileload to first read the file into a variable.

Advanced Features πŸ”§

Struct Annotations
Uniface automatically adds metadata that provides information about the JSON structure:

  • jsonClass: Type of JSON construction (object or array)

  • jsonDataType: Data type of the struct member

Character Handling
During conversion, line breaks are normalized:

\r (Carriage Return) and \n (Linefeed) are converted to \r

When converting back with structToJson, CR characters are written as \n

Best Practices 🎯

  1. Always implement error handling: Check the $status value after each conversion
  2. Consider file format: Ensure JSON files are Unicode-encoded
  3. Test complex structures: For nested JSON objects, test access paths beforehand
  4. Documentation: Comment complex JSON structures for better maintainability

Comparison with Other Languages 🌐
Other programming languages have similar functions:

  • JavaScript: JSON.parse()

  • Python: json.loads()

  • C#: JsonSerializer.Deserialize()

What makes Uniface's jsonToStruct special is its seamless integration into the Uniface development environment and automatic struct generation.

Conclusion πŸŽ‰
The jsonToStruct function is a powerful tool for Uniface developers that significantly simplifies working with JSON data. It offers:

  • Simple syntax for quick implementation

  • Automatic type recognition for various JSON data types

  • Robust error handling for production-ready applications

  • Flexible data sources (strings, variables, files)

For anyone working with Uniface and needing to process JSON data, this function is definitely worth a closer look. It makes integrating web APIs and processing modern data formats much easier.

Have you already had experience with jsonToStruct? Share your tips and tricks in the comments! πŸ’¬

Transparency Note: This post was completely generated with AI, based on the Uniface Documentation 10.4. The prompts, ideas, and content direction come from the author to ensure a practical and understandable presentation of the technical concepts.

Top comments (0)