Generating JSON output with Unibase reports (ubreport
) might seem straightforward, as JSON is essentially a text-based format. However, as this topic will explore, simply structuring a report to resemble JSON can easily result in invalid syntax, particularly concerning trailing commas within arrays or objects. This introduction will highlight a common pitfall when aiming for JSON output with ubreport
and then demonstrate a specific technique using the N
section to ensure the generated output adheres to valid JSON standards, making it readily parsable by other applications.
If you just write a simple report:
: names K name H [ > F ] > R "name": "[name]", >
You will get something like:
[ "name":"fred", "name":"jane", ]
The problem here is the final “,” (“jane”,). This will break many JSON parsers.
To fix this we add an N section:
: names K name H [ > F ] > N [name] , > R "name": "[name]" >
Now you will get something like:
[ "name":"fred" , "name":"jane" ]
Which is legal JSON.