Sheerpower Logo

F.8  JSON Related Functions


JSON Related Functions

JSON (JavaScript Object Notation) is a lightweight data format commonly used for web APIs and data exchange. SheerPower provides two powerful functions for working with JSON data: JSON$() for converting SheerPower clusters into JSON strings, and jsonutil$() for parsing and manipulating existing JSON data. Together, these functions make it easy to integrate SheerPower applications with web services and modern data workflows.

JSON$() - Converting Clusters to JSON

The JSON$() function converts SheerPower cluster arrays into properly formatted JSON strings.

Syntax

string$ = JSON$(clustername [, row_option])

Where:

  • clustername - The cluster to convert
  • row_option - Optional row number. Use -1 for all rows (default)

Example 1: Simple Cluster Array

Convert a basic cluster array to JSON:

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_output$ = JSON$(person) print json_output$

Output:

[ {"id$":"1", "name$":"Alice", "age":30}, {"id$":"2", "name$":"Bob", "age":25} ]

Example 2: Nested Cluster Array

Convert clusters with nested structures:

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_output$ = JSON$(person) print json_output$

Output:

[ { "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" } } ]

Example 3: Single Row Output

Extract just one row as JSON:

// Using the person cluster from above single_person$ = JSON$(person, 1) // Get first row only print single_person$

Output:

{"id$":"1", "name$":"Alice", "age":30, "address":{"street$":"123 Main St", "city$":"Springfield", "zip$":"12345"}}

jsonutil$() - Working with JSON Data

The jsonutil$() function parses, extracts, and manipulates JSON data using JSON Pointer syntax.

Syntax

result$ = jsonutil$(json_input$ [, json_pointer$])

Where:

  • json_input$ - The JSON string to process
  • json_pointer$ - Optional JSON Pointer path to extract specific values

Example 1: Extracting Values

Extract specific values from JSON:

json_data$ = '{"name":"Alice","age":30,"city":"Springfield"}' name$ = jsonutil$(json_data$, '/name') age$ = jsonutil$(json_data$, '/age') city$ = jsonutil$(json_data$, '/city') print "Name: "; name$ print "Age: "; age$ print "City: "; city$

Output:

Name: Alice Age: 30 City: Springfield

Example 2: Working with Arrays

Extract values from JSON arrays:

json_array$ = '[{"name":"Alice","age":30},{"name":"Bob","age":25}]' first_name$ = jsonutil$(json_array$, '/0/name') second_age$ = jsonutil$(json_array$, '/1/age') print "First person: "; first_name$ print "Second person age: "; second_age$

Output:

First person: Alice Second person age: 25

Example 3: Getting All Keys

Extract all keys from a JSON object:

json_data$ = '{"name":"Alice","age":30,"city":"Springfield"}' keys$ = jsonutil$(json_data$, '') print "Available keys: "; keys$

Output:

Available keys: ["name","age","city"]

Example 4: Converting JSON to Clusters

Convert JSON data back into SheerPower clusters:

cluster employee: name$, age, department$ json_data$ = '[ {"name":"Alice","age":30,"department":"Engineering"}, {"name":"Bob","age":25,"department":"Sales"}, {"name":"Carol","age":35,"department":"Marketing"} ]' jsonutil$(json_data$, 'to_cluster:employee') print "Employees:" for each employee print employee->name$; " ("; employee->age; ") - "; print employee->department$ next employee

Output:

Employees: Alice (30) - Engineering Bob (25) - Sales Carol (35) - Marketing

Practical Use Cases

Web API Integration

Combine both functions for complete API workflows:

// Prepare data to send to an API cluster order: id$, product$, quantity, price add cluster order order->id$ = "ORD001" order->product$ = "Widget" order->quantity = 5 order->price = 12.99 // Convert to JSON for API request api_request$ = JSON$(order, 1) print "Sending to API: "; api_request$ // Simulate API response api_response$ = '{"status":"success","order_id":"ORD001","total":64.95}' // Extract response data status$ = jsonutil$(api_response$, '/status') total$ = jsonutil$(api_response$, '/total') print "API Status: "; status$ print "Order Total: $"; total$
Note: These JSON functions make SheerPower applications fully compatible with modern web services, REST APIs, and JSON-based data exchange formats. The combination of JSON$() and jsonutil$() provides complete round-trip JSON support.
Hide Description

    

       
Results are shown here.

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.