Initializing help system before first use

jsonparse

Purpose
Parse a JSON document.
Synopsis
function jsonparse(afct:array(range) of string|function,ctx:ctxtype): integer
Arguments
afct 
Event function table. Each entry of this array is the name of or a reference to the function to call when the corresponding event occurs. The expected events are (all of these entries are optional):
JSON_FCT_OPEN_OBJ 
Opening of an object
JSON_FCT_CLOSE_OBJ 
Closing of an object
JSON_FCT_OPEN_ARR 
Opening of an array
JSON_FCT_CLOSE_ARR 
Closing of an array
JSON_FCT_TEXT 
A textual value
JSON_FCT_NUM 
A numerical value
JSON_FCT_BOOL 
A Boolean value
JSON_FCT_NULL 
The null value
ctx 
Value passed as firt argument of all event functions
Return value
0 if successful, 1 in case of parsing error or a non-zero value returned by an event function
Example
This example displays values of object members "name" and "age" of a JSON document:
declarations
 afct:array(range) of string
  s_ctx=record
   cnt:integer
  end-record
  c:s_ctx
end-declarations

public function setvalue_all(ctx:s_ctx,name:text,type:integer,val:text):integer
 if name="name" or name="age" then
  writeln(name,":",val)
  ctx.cnt+=1
 end-if
end-function

afct(JSON_FCT_TEXT):="setvalue_all"   ! A value as a text
fopen("mydoc.json",F_INPUT)
rts:=jsonparse(afct,c)
fclose(F_INPUT)
writeln("line count:",c.cnt)
Further information
1. This function is an alternative approach to jsonload for processing JSON documents: instead of loading into memory the entire document this function calls a dedicated routine whenever it identifies a JSON entity. For instance a specific function is called when an object is open and another one when it is closed. It is up to the Mosel program to decide how to handle the document via these event handling functions.
2. To each event type corresponds a specific function signature. These functions return an integer that decides whether parsing should continue: a non-zero value will cause the parsing to cancel (this value is used as the return value of jsonparse). The expected function signatures are:
JSON_FCT_OPEN_OBJ
function open_object(ctx:ctxtype, name:text):integer
JSON_FCT_CLOSE_OBJ
function close_object(ctx:ctxtype):integer
JSON_FCT_OPEN_ARR
function open_array(ctx:ctxtype, name:text):integer
JSON_FCT_CLOSE_ARR
function close_array(ctx:ctxtype):integer
JSON_FCT_TEXT
function text_val(ctx:ctxtype, name:text, type:integer, val:text):integer
JSON_FCT_NUM
function num_val(ctx:ctxtype, name:text, val:real):integer
JSON_FCT_BOOL
function bool_val(ctx:ctxtype, name:text, val:boolean):integer
JSON_FCT_NULL
function null_val(ctx:ctxtype, name:text):integer
In addition to the pre-defined arguments these functions take a context as their first parameter. This variable (that can be of any type) is provided to the jsonparse routine and can be used by the event functions for storing progress information.
The name argument is not empty only when the value corresponds to an object member: in this case this parameter is the label of this member. The type argument passed to the text_val function indicates the type of the data (0 for null, 1 for text, 2 for numerical and 3 for Boolean): this function is used with the textual representation of the value when the required type-specific function is not available. For instance this function will be called with type=3 if a Boolean value has been read and the entry JSON_FCT_BOOL is not defined in the function table.
3. An error message indicating the location of the error is displayed when the parsing fails or if an event function returns a negative value (a positive value also interrupts parsing but no message is displayed).
Related topics
Module

© 2001-2025 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.