|
F.8 JSON Related Functions
|
string$ = json$(clustername [,row_option]])
Given a cluster and optional row number, the JSON$() function generates the
appropriate JSON string for the given cluster and row. If the row given is -1,
all rows are returned.
The JSON$() function in sheerpower is used to create
JSON-formatted strings from sheerpower cluster arrays. This function is
particularly useful for creating JSON data to be used in web applications or
for API communications.
Syntax
The basic syntax of the JSON$() function is as follows:
string$ = JSON$(clustername [,row_option])
Given a cluster and an optional row number, the JSON$() function generates
the appropriate JSON string for the given cluster and row. If the row
given is -1, all rows are returned.
Examples
Example 1: Cluster Array
Convert a cluster array to a JSON string:
cluster person: id$, name$, age
add cluster person
person->id$ = "1"
person->name$ = "Alice"
person->age = 30
add cluster person
person->id$ = "2"
person->name$ = "Bob"
person->age = 25
json_people$ = JSON$(person)
print json_people$
This will produce the following JSON string:
[
{"id$":"1", "name$":"Alice", "age":30},
{"id$":"2", "name$":"Bob", "age":25}
]
Example 2: Nested Cluster Array
Convert a nested cluster array to a JSON string:
cluster address: street$, city$, zip$
cluster person: id$, name$, age, address->address
add cluster person
person->id$ = "1"
person->name$ = "Alice"
person->age = 30
person->address->street$ = "123 Main St"
person->address->city$ = "Springfield"
person->address->zip$ = "12345"
add cluster person
person->id$ = "2"
person->name$ = "Bob"
person->age = 25
person->address->street$ = "456 Elm St"
person->address->city$ = "Shelbyville"
person->address->zip$ = "67890"
json_people$ = JSON$(person)
print json_people$
This will produce the following JSON string:
[
{"id$":"1", "name$":"Alice", "age":30, "address":{"street$":"123 Main St", "city$":"Springfield", "zip$":"12345"}},
{"id$":"2", "name$":"Bob", "age":25, "address":{"street$":"456 Elm St", "city$":"Shelbyville", "zip$":"67890"}}
]
The JSON$() function is versatile and can handle complex data structures, making it a powerful tool for developers working with web technologies and APIs in sheerpower.
cluster name: first$, last$
cluster multi: ssn$, prefix cluster name, tax, is_okay?
When the JSON$() function is called, it will output a JSON formatted string
showing
each row and object within the cluster.
Syntax
The basic syntax of the jsonutil$() function is as follows:
json_result$ = jsonutil$(json_input$ [, json_pointer$])
Where json_input$ is the JSON string to be processed, and json_pointer$ is an optional parameter that defines the specific value to be extracted using JSON Pointer syntax.
Examples
Example 1: Extracting a Value
Extract a specific value from a JSON string:
json_data$ = '{"name":"Alice","age":30}'
name$ = jsonutil$(json_data$, '/name')
print name$
This will output:
Alice
Example 2: Checking if a Key Exists
Check if a specific key exists in a JSON string:
json_data$ = '{"name":"Alice","age":30}'
exists? = jsonutil$(json_data$, '/name') <> ''
print exists$
This will output:
true
Example 3: Extracting All Keys
Extract all keys from a JSON string:
json_data$ = '{"name":"Alice","age":30}'
keys$ = jsonutil$(json_data$, '')
print keys$
This will output:
["name","age"]
Example 4: Converting JSON to a Cluster Array
Convert JSON data into a sheerpower cluster array:
cluster person: name$, age
json_data$ = '[{"name":"Alice","age":30},{"name":"Bob","age":25}]'
jsonutil$(json_data$, 'to_cluster:person')
for each person
print person->name$, person->age
next person
This will output:
Alice 30
Bob 25
The jsonutil$() function provides a versatile way to work with
JSON data in sheerpower, enabling easy extraction, manipulation, and
conversion of JSON data into formats suitable for further processing
within sheerpower programs.