Confira a baixo um exemplo simples de como converter String para um Objeto Json e o manipular em OpenEdge Progress.
Para tornar o exemplo mais completo vamos desmembrar uma string JSon mais complexo em Objetos menores:
USING Progress.Json.ObjectModel.*.
define VARIABLE myParser AS ObjectModelParser NO-UNDO.
DEFINE VARIABLE objMessage AS CLASS JSONObject NO-UNDO.
DEFINE VARIABLE objRetornoDD AS CLASS JSONObject NO-UNDO.
DEFINE VARIABLE objVeiculo AS CLASS JSONObject NO-UNDO.
DEFINE VARIABLE strJson AS CHARACTER NO-UNDO.
assign strJson =
'~{"organizationId":"6878e6b48d2f",
"workOrderId":"21a8816e5f31",
"value":~{
"workOrderCreated":~{
"organizationId":"6878e6b48d2f",
"workOrderId":"21a8816e5f31"
}
},
"evento":"workOrderCreated",
"dhEvento":"2021-03-19 10:49:17",
"getRetorno":~{
"actualDropOffTime":null,
"actualPickupTime":null,
"customerName":"TRANSPORTES LTDA",
"id":"21a8816e5f31",
"mileage":19900,
"organizationId":"6878e6b48d2f",
"plannedDate":"2021-04-07",
"product":~{
"registrationNumber":"TTB9999",
"active":true,
"serialNumber":"1234567",
"vin":"TEMPTABLE1234567"
}
}
}'.
myParser = NEW ObjectModelParser().
/* Cinvertemos toda a string em um unico objeto */
objMessage = CAST(myParser:Parse(strJson),JsonObject).
/* Agora vamos extrair getRetorno e do getRetorno extrair o product */
objRetornoDD = CAST(myParser:Parse(string(objMessage:GetJsonText("getRetorno"))),JsonObject).
objVeiculo = CAST(myParser:Parse(string(objRetornoDD:GetJsonText("product"))),JsonObject).
MESSAGE
"OrganizationID: " string(objMessage:GetJsonText("organizationId"))
skip
"workOrderId: " string(objMessage:GetJsonText("workOrderId"))
skip
"Evento: " string(objMessage:GetJsonText("evento"))
skip
"CustomerName: " string(objRetornoDD:GetJsonText("customerName"))
skip
"SerialNumber: " string(objVeiculo:GetJsonText("serialNumber"))
VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.
O resultado será este:
Links relacionados:
https://knowledgebase.progress.com/articles/Article/How-to-turn-a-string-into-a-JsonObject