JSON serialization and deserialization
Parses JSON text into an object and serializes it back.
Overview
FromJson populates a JsonObject and returns Nil on success or error text on failure; its return is not the populated object. Read properties only after checking that result. ToJson produces text whose property order may differ from the input. Valid syntax does not establish a valid business payload: check fields, types, sizes and authorization. Dates need an explicit contract format independent of Protheus display settings. The example uses fictional data and does not log external input. (Sources: refs. 1–3.)
Commented example
#Include "Protheus.ch"
User Function Ed647()
Local oData := JsonObject():New()
Local xError := oData:FromJson('{"id":"ED647","active":true}')
Local cJson := ""
// Nil means success; text means a parse failure.
If ValType(xError) == "U"
cJson := oData:ToJson()
ConOut(cJson)
Else
ConOut("INVALID_JSON")
EndIf
FreeObj(oData)
Return NilUnder the documented contract serializes id and active; property order is not guaranteed. No database access occurs. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.
- Test invalid JSON, absent fields, null and wrong types. Do not concatenate unescaped input into JSON or expose secrets in errors.
- 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.