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
Parameters:
StructTarget:
Variable, parameter, or non-database field to hold the generated structureJsonSource:
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#
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
}
Struct members are addressable by name: vStruct->name
JSON Arrays
Use square brackets [ ] and contain indexed values:
["JavaScript", "Python", "C#", "Uniface"]
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
Working with Files π
The function can also process JSON files directly:
jsonToStruct vStruct, "data.json"
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 π―
- Always implement error handling: Check the $status value after each conversion
- Consider file format: Ensure JSON files are Unicode-encoded
- Test complex structures: For nested JSON objects, test access paths beforehand
- 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)