π€ This blog post was created with AI assistance to provide comprehensive technical guidance based on official Uniface 10.4 documentation.
The structToJson
statement in Uniface 10.4 is a powerful ProcScript function that converts complex data structures (Structs) into JSON format π. This functionality is essential for modern web applications, API integrations, and data exchange between different systems.
π Basic Syntax and Usage
The basic syntax for structToJson
is straightforward:
structToJson {/bmp} {/whitespace} JsonTarget, StructSource
Parameters explained:
- JsonTarget: A string variable, parameter, or field that will hold the resulting JSON text π
- StructSource: The Struct variable or parameter containing the data to be converted ποΈ
π― Practical Example
Here's a complete example showing how to use structToJson
:
variables
vStruct : struct
vJsonOutput : string
endvariables
; Create a sample struct
vStruct->name = "John Developer"
vStruct->age = 30
vStruct->skills->{1} = "C#"
vStruct->skills->{2} = "Uniface"
vStruct->skills->{3} = "SQL"
vStruct->active = "T"
; Convert struct to JSON
structToJson /whitespace vJsonOutput, vStruct
; Output the result
putmess vJsonOutput
This example creates a Struct with personal information and converts it to readable JSON format π¨.
π§ Qualifiers: Controlling Output Format
The structToJson
statement supports two important qualifiers:
/whitespace Qualifier
The /whitespace
qualifier formats the JSON with indentation and line breaks for better readability π:
; Without /whitespace:
{"name":"John","skills":["C#","Uniface"]}
; With /whitespace:
{
"name" : "John",
"skills" :
[
"C#",
"Uniface"
]
}
/bmp Qualifier
The /bmp
qualifier ensures that Unicode characters outside the Basic Multilingual Plane (BMP) are properly escaped π. This is important for compatibility with systems that only support UCS-2 encoding.
π Understanding JSON Structure Types
The structToJson
function can generate two main JSON structure types:
JSON Objects
When all Struct members have names, they are converted to JSON objects using curly braces {}
ποΈ:
; Struct with named members
vPerson->firstName = "Maria"
vPerson->lastName = "Schmidt"
; Results in JSON object:
{
"firstName" : "Maria",
"lastName" : "Schmidt"
}
JSON Arrays
When Struct members don't have names or are indexed, they become JSON arrays using square brackets []
π:
; Struct with indexed members
vSkills->{1} = "JavaScript"
vSkills->{2} = "Python"
vSkills->{3} = "Go"
; Results in JSON array:
["JavaScript", "Python", "Go"]
β οΈ Error Handling and Status Codes
Proper error handling is crucial when using structToJson
. The function returns different status codes:
structToJson vJsonOutput, vStruct
if ($status = 0)
putmess "β
Conversion successful!"
; Use filedump to save to file if needed
filedump "output.json", vJsonOutput
else
putmess "β Conversion failed: " ; $procerror
endif
Common status codes:
-
0
: Successful conversion β -
< 0
: Conversion failed - check$procerror
for details β
Typical errors include:
-
-1905 (STRUCTERR_INPUT)
: Invalid input struct data π«
π¨ Advanced Features and Annotations
Uniface automatically handles various data types and applies appropriate JSON formatting:
Data Type Mapping
- Strings: Enclosed in double quotes with proper escaping π
- Numbers: Written as numeric values without quotes π’
- Booleans: Converted to JSON true/false values βοΈ
- Special Characters: Properly escaped (quotes, backslashes, GOLD characters) π
Struct Annotations
You can use $tags
to set jsonClass
annotations that control how Structs are converted:
-
object
: Forces conversion to JSON object ποΈ -
array
: Forces conversion to JSON array π -
value
: Treats as simple value π
πΎ Working with Files
To save JSON output to a file, use the filedump
statement:
structToJson vJsonOutput, vStruct
if ($status = 0)
; Save to local file
filedump "data.json", vJsonOutput
; Or save to server path
lfiledump "/path/to/server/data.json", vJsonOutput
endif
π Best Practices
- Always implement error handling: Check
$status
after each conversion π‘οΈ - Use meaningful variable names: Makes code more maintainable π
- Consider output format: Use
/whitespace
for debugging, omit for production π― - Test complex structures: Verify nested objects and arrays work as expected π§ͺ
- Handle Unicode properly: Use
/bmp
when needed for compatibility π - Document your Struct structure: Comment complex data transformations π
π Integration with Other Uniface Features
The structToJson
function works seamlessly with other Uniface data transformation features:
- componentToStruct: Convert form data to Struct, then to JSON π
- xmlToStruct: Transform XML data via Struct to JSON π
- jsonToStruct: Round-trip conversion for data processing π
π― Real-World Use Cases
API Integration: Convert Uniface form data to JSON for REST API calls π
Data Export: Export database records as JSON for external systems π€
Configuration Files: Generate JSON configuration from Uniface parameters βοΈ
Web Applications: Send structured data to JavaScript front-ends π»
π Conclusion
The structToJson
statement in Uniface 10.4 is an essential tool for modern application development π. It provides:
- Simple syntax for quick implementation β‘
- Flexible output formatting options π¨
- Robust error handling for production use π‘οΈ
- Seamless integration with Uniface development workflow π
Whether you're building web APIs, integrating with external systems, or processing complex data structures, structToJson
makes working with JSON data in Uniface straightforward and reliable πͺ.
Have you used structToJson
in your Uniface projects? Share your experiences and tips in the comments below! π¬
π€ Transparency Note: This blog post was created with AI assistance based on official Uniface 10.4 documentation to provide accurate and comprehensive technical guidance.
Top comments (0)