Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

JSON.CreateObjectRef

Creates empty object reference.

Component Version macOS Windows Linux Server iOS SDK
JSON 6.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "JSON.CreateObjectRef" )

Parameters

none

Result

Returns reference number or error.

Description

Creates empty object reference.
Please use JSON.Release later to free the json object.
You can pass the reference number to all MBS JSON functions instead of JSON text.

Examples

Try object reference functions:

# create object reference
$ref = MBS( "JSON.CreateObjectRef")
# add some texts
$r = MBS("JSON.AddStringToObject"; $ref; "FirstName"; "Joe"; "LastName"; "Miller")
# add some number
$r = MBS("JSON.AddNumberToObject"; $ref; "Age"; 50)
# add a true value:
$r = MBS( "JSON.AddTrueToObject"; $ref; "married" )
# get the text
$text = MBS( "JSON.Format"; $ref)
# free memory
$r = MBS( "JSON.Release"; $ref)

Create JSON with values in fields on current layout:

Let ( [
    // Create object as reference
    j = MBS( "JSON.CreateObjectRef" );
    // List fields in current layout
    fields = FieldNames ( get(FileName) ; get(LayoutName ));
    // how many?
    c = ValueCount ( fields );
    // make a loop to add field values
    r = While ( i = 1 ; i ≤ c ; [ name = GetValue ( fields ; i ); r = MBS( "JSON.AddItemToObject"; j; name; MBS( "JSON.CreateValue"; GetField ( name ) )); i = i + 1 ] ; i );
    // format the result
    text = MBS( "JSON.Format"; j );
    // free json
    r = MBS( "JSON.Release"; j )
];
// return result as text
  text )

Example result:
{ "First": "John", "Last": "Miller", "Group": "", "Company": "Some Ltd.", "ID": 12 }

Create complex JSON:

Set Variable [ $a ; Value: MBS( "JSON.CreateArrayRef" ) ]
#
# first entry
Set Variable [ $o ; Value: MBS( "JSON.CreateObjectRef" ) ]
Set Variable [ $r ; Value: MBS( "JSON.AddNumberToObject"; $o; "FieldType"; 11 ) ]
Set Variable [ $r ; Value: MBS( "JSON.AddNullToObject"; $o; "FieldName" ) ]
Set Variable [ $r ; Value: MBS( "JSON.AddStringToObject"; $o; "Value"; "Julien" ) ]
Set Variable [ $r ; Value: MBS( "JSON.AddBooleanToObject"; $o; "EmptyMatch"; False ) ]
Set Variable [ $r ; Value: MBS( "JSON.AddItemToArray"; $a; $o) ]
Set Variable [ $r ; Value: MBS( "JSON.Release"; $o) ]
#
# second entry
Set Variable [ $o ; Value: MBS( "JSON.CreateObjectRef" ) ]
Set Variable [ $r ; Value: MBS( "JSON.AddNumberToObject"; $o; "FieldType"; 23 ) ]
Set Variable [ $r ; Value: MBS( "JSON.AddNullToObject"; $o; "FieldName" ) ]
Set Variable [ $r ; Value: MBS( "JSON.AddStringToObject"; $o; "Value"; "Stadel" ) ]
Set Variable [ $r ; Value: MBS( "JSON.AddBooleanToObject"; $o; "EmptyMatch"; False ) ]
Set Variable [ $r ; Value: MBS( "JSON.AddItemToArray"; $a; $o) ]
Set Variable [ $r ; Value: MBS( "JSON.Release"; $o) ]
#
# surrounding object
Set Variable [ $o ; Value: MBS( "JSON.CreateObjectRef" ) ]
Set Variable [ $r ; Value: MBS( "JSON.AddItemToObject"; $o; "Data"; $a) ]
Set Variable [ $r ; Value: MBS( "JSON.AddNumberToObject"; $o; "Id"; 123 ) ]
Set Variable [ $r ; Value: MBS( "JSON.Release"; $a) ]
#
# put in final object
Set Variable [ $j ; Value: MBS( "JSON.CreateObjectRef" ) ]
Set Variable [ $r ; Value: MBS( "JSON.AddItemToObject"; $j; "Address"; $o) ]
Set Variable [ $r ; Value: MBS( "JSON.Release"; $o) ]
#
Set Field [ JSON::JSON ; MBS("JSON.Colorize"; MBS("JSON.Format"; $j)) ]
Set Variable [ $r ; Value: MBS( "JSON.Release"; $j) ]

Example result:
{ "Address": { "Data": [ { "FieldType": 11, "FieldName": null, "Value": "Julien", "EmptyMatch": false }, { "FieldType": 23, "FieldName": null, "Value": "Stadel", "EmptyMatch": false } ], "Id": 123 } }

See also

Example Databases

Blog Entries

This function is free to use.

Created 5th January 2016, last changed 20th November 2019


JSON.CreateObject - JSON.CreateString