McBasic™Get McBasic™ ↗
Build 881
Language reference

JSON values & HTTP envelopes

Read structured data by shape, and distinguish transport results from the response body.

JsonValue is a value, not an object reference#

Assign a JsonValue with =, not Set. Json.Parse converts text to a structured value. Check its Kind and expected members before converting scalar data into your own types. A property that is absent and a property explicitly set to JSON null are different cases.

The small navigation API#

OperationPurpose
Json.Parse(text)Parse JSON text.
Json.Has(value, name)Check for an object member.
Json.Get(value, name)Read the named member.
Json.At(value, index)Read a zero-based array item.
Json.Count(value)Count array items or object properties.
Json.Kind(value)Inspect Object, Array, String, Number, Boolean or Null.
Json.Value(value)Convert a scalar to a BASIC value.
Json.Stringify(value)Serialize a JsonValue to text.

Parse a known document first#

A complete procedure with no network dependency
Public Sub ReadLocalJson()
    Dim document As JsonValue
    Dim name As String
    document = Json.Parse("{""name"":""Ada""}")
    If Json.Has(document, "name") Then
        name = CStr(Json.Value(Json.Get(document, "name")))
        Debug.Print name
    End If
End Sub

This fixed input prints Ada. For untrusted input, also inspect the root and member kinds and handle parse/type errors. Has alone does not prove that a value is a String.

Parse the response envelope, then the body#

Http.RequestAsync returns a JSON envelope as text, not the remote document directly. Inspect the response status before interpreting body as success data. The body is text: parse it separately if the service promises JSON. An HTML error page is not a malformed successful API response; it is a different result that needs a useful message.

The HTTP guide and WeatherDesk tutorial include the real request and response handling. The downloadable web editions check HTTP status before parsing the service body.

Test the shape you do not want#

Test an empty array before reading item zero, a missing optional property, JSON null, a wrong scalar kind and invalid JSON. Preserve previous valid UI state when a refresh fails. Do not put credentials in source code or a public tutorial archive.

Source baseline 20f4bfed6b · Build manifest
Sources & maintenance

Original web explanation, reviewed against these repository sources. API tables and example files follow the implementation.

  • docs/manual/21-language-reference.md
  • docs/manual/11-language-toolkit.md
  • site/examples/LanguageWorkbench/Main.bas