JsonObject and JSON handling
JsonObject():New(); o:FromJson(cJson); o:ToJson()Parses JSON, accesses properties and serializes the object.
Overview
JsonObject creates dynamic properties accessible through brackets. FromJson returns NIL on success and error text on failure; the official Character type table does not override that rule. Check parsing before accessing the object, then validate the expected schema. ToJson returns a JSON string without fixed property order guarantees. Documentation records P12 build 131227a from version 13.2.3.6. (Sources: refs. 1–3.)
Syntax
JsonObject():New(); o:FromJson(cJson); o:ToJson()Methods
NewCreates an empty object.JsonObject():New()
FromJsonLoads JSON text; NIL indicates success.o:FromJson(cJson)
ToJsonExports the object as a string.o:ToJson() -> cJson
Commented example
#Include "Protheus.ch"
User Function Ed501()
Local oJson := JsonObject():New()
Local uError := oJson:FromJson('{"code":"ED01","quantity":3}')
// NIL represents successful parsing, not failure.
If ValType(uError) == "U"
ConOut(oJson:ToJson())
Else
ConOut(cValToChar(uError))
EndIf
FreeObj(oJson)
Return NilSerializes the fictional document when parsing succeeds. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.
- Validate field presence and types, size limits and contents before applying rules. Do not log sensitive payloads in full.
- Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Comments
There are no approved comments yet.
Sign in with Google or Microsoft to comment.