Xbasic

Extension::JsonJsonForceObjectArray Method

Syntax

.JsonForceObjectArray as c (json as C [, fieldName as C])

Arguments

jsonCharacter

Json to process arrays of non objects.

fieldNameCharacter

 

Description

Arrays of type other than object are wrapped in an object.

Discussion

In the example below, the 'experience' array has strings , this function will quickly wrap all the strings in an object with a pprovided name (if name is ommitted, the name __value is defaulted).

Example

dim json as c = <<%json%
[
   {
       "programmer" : "Fred" ,
       "experience" : [ "C++" , "Java" , "C#" , "Javascript" ]
   },
   {
       "programmer" : "Joe" ,
       "experience" : [ "C#" , "Javascript" , "Python" , "Go" ]
   }   
]
%json%

dim out as c = extension::json::JsonForceObjectArray(json) 
? json_reformat(out)
[
    {
        "programmer": "Fred",
        "experience": [
            {
                "language": "C++"
            },
            {
                "language": "Java"
            },
            {
                "language": "C#"
            },
            {
                "language": "Javascript"
            }
        ]
    },
    {
        "programmer": "Joe",
        "experience": [
            {
                "language": "C#"
            },
            {
                "language": "Javascript"
            },
            {
                "language": "Python"
            },
            {
                "language": "Go"
            }
        ]
    }
]