LIB_JSON_SORT library containing SORT_JSON_ARRAY function defining a generic sort mechanism for JSON files. This can be used in ASTER scripts to sort JSON files for example before comparison by Int4 Suite in case there is no guarantee on the nodes sequence.
#Int4 KB sample — AS IS, not official docs, no support. Test before use.
# ─────────────────────────────────────────────────────────────
# LIB_JSON_SORT
# Reusable function: sort a JSON array node by a given field,
# ascending or descending, and write the result back in-place.
#
# SORT_JSON_ARRAY(TREE, PATH, FIELD, DIR)
# TREE : parsed JSON tree (from JSON_PARSE)
# PATH : list of keys to navigate to the array, e.g. ("ROOT","ITEMS")
# FIELD: the field name to sort by, e.g. "BRAND_CODE"
# DIR : "ASC" or "DESC"
# ─────────────────────────────────────────────────────────────
DEFUN("SORT_JSON_ARRAY", ("TREE", "PATH", "FIELD", "DIR"),
LOCALS(("ARR", "SORTED", "NODE", "I"),
# Navigate to the target node by walking PATH keys
NODE = TREE;
FOR(I = 1, I <= COUNT(PATH), I = I + 1,
NODE = NODE[PATH[I]]
);
ARR = NODE;
# Sort ascending or descending based on DIR
IF(DIR $== "ASC",
SORTED = QSORT(ARR, LAMBDA(("A","B"), A[FIELD] < B[FIELD])),
SORTED = QSORT(ARR, LAMBDA(("A","B"), A[FIELD] > B[FIELD]))
);
# Write sorted array back into TREE at the correct path
IF(COUNT(PATH) == 1,
TREE[PATH[1]] = SORTED
);
IF(COUNT(PATH) == 2,
TREE[PATH[1]][PATH[2]] = SORTED
);
IF(COUNT(PATH) == 3,
TREE[PATH[1]][PATH[2]][PATH[3]] = SORTED
);
IF(COUNT(PATH) == 4,
TREE[PATH[1]][PATH[2]][PATH[3]][PATH[4]] = SORTED
);
TREE # return the modified tree
)
);
#=============================================================================
# SUPPLEMENTARY SAMPLE — NOT OFFICIAL DOCUMENTATION
#=============================================================================
# Provided AS IS to active customers via the Int4 Knowledge Base.
# No warranty, no SLA, no support coverage. Test in a non-production
# environment before use. You are responsible for any consequences of
# running or modifying this code.
#=============================================================================
Example usage in ASTER:
#Int4 KB sample — AS IS, not official docs, no support. Test before use.
INCLUDE("LIB_JSON_SORT");
JSON = `{
"root": {
"header": {
"title": "Sample Document",
"author": "John Doe",
"date": "2026-05-28"
},
"items": [
{
"id": 1,
"name": "Item One",
"value": 100
},
{
"id": 2,
"name": "Item Two",
"value": 200
},
{
"id": 3,
"name": "Item Three",
"value": 300
}
]
}
}`;
TREE = JSON_PARSE(JSON);
TREE = SORT_JSON_ARRAY(
TREE,
("root", "items"),
"id",
"DESC"
);
JSON_RENDER(TREE)
#=============================================================================
# SUPPLEMENTARY SAMPLE — NOT OFFICIAL DOCUMENTATION
#=============================================================================
# Provided AS IS to active customers via the Int4 Knowledge Base.
# No warranty, no SLA, no support coverage. Test in a non-production
# environment before use. You are responsible for any consequences of
# running or modifying this code.
#=============================================================================