DataFlex
DataFlex
Sort JSON Object by Member Key Name
See more JSON Examples
Demonstrates how to sort the members of a JSON object by the key name.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Handle hoJson
Boolean iSuccess
Boolean iAscending
Boolean iCaseSensitive
Variant vJsonAbc
Handle hoJsonAbc
String sTemp1
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get ComUpdateString Of hoJson "xyz" "1234" To iSuccess
Get ComUpdateString Of hoJson "abc.xyz" "1234" To iSuccess
Get ComUpdateString Of hoJson "abc.def" "1234" To iSuccess
Get ComUpdateString Of hoJson "abc.aaa" "1234" To iSuccess
Get ComUpdateString Of hoJson "ghi" "1234" To iSuccess
Get ComUpdateString Of hoJson "nmo" "1234" To iSuccess
Set ComEmitCompact Of hoJson To False
Get ComEmit Of hoJson To sTemp1
Showln sTemp1
// This is our initial JSON:
// {
// "xyz": "1234",
// "abc": {
// "xyz": "1234",
// "def": "1234",
// "aaa": "1234"
// },
// "ghi": "1234",
// "nmo": "1234"
// }
// Sort the top-level JSON object by key.
Move True To iAscending
Move True To iCaseSensitive
Send ComSort To hoJson iAscending iCaseSensitive
// Look at the sorted JSON..
Get ComEmit Of hoJson To sTemp1
Showln sTemp1
// {
// "abc": {
// "xyz": "1234",
// "def": "1234",
// "aaa": "1234"
// },
// "ghi": "1234",
// "nmo": "1234",
// "xyz": "1234"
// }
// Now sort the members of the "abc" object..
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonAbc
If (Not(IsComObjectCreated(hoJsonAbc))) Begin
Send CreateComObject of hoJsonAbc
End
Get pvComObject of hoJsonAbc to vJsonAbc
Get ComObjectOf2 Of hoJson "abc" vJsonAbc To iSuccess
Send ComSort To hoJsonAbc iAscending iCaseSensitive
// Now look at the JSON with the members under "abc" also sorted..
Get ComEmit Of hoJson To sTemp1
Showln sTemp1
// {
// "abc": {
// "aaa": "1234",
// "def": "1234",
// "xyz": "1234"
// },
// "ghi": "1234",
// "nmo": "1234",
// "xyz": "1234"
// }
End_Procedure