JSON-Datentypen und -Funktionen
- Aktualisiert2026-07-22
- 2 Minute(n) Lesezeit
Verwenden Sie die PAscript-JSON-Funktionen und die Datentypen zum Erstellen von JSON-Strings.
Einführung in PAtools 2025 Q2
Beispiel: Zugriff auf SystemLink-API-Endpunkte mit einem JSON-Body
Hinweis Im folgenden Beispiel wird davon ausgegangen, dass im Test der Name OrgDaten_SystemLink_TestPlanId.TXT definiert ist, der die Testplan-ID für SystemLink enthält.
Hinweis Weitere Informationen zur Verwendung der HTTP-API mit sys:systemLink:request finden Sie in der HTTP-API-Referenz in der SystemLink - API-Referenz.
program
section globals
text OrgDaten_SystemLink_TestPlanId.TXT
endsection
// 1. Get the test plan
int32 httpStatusCode
jsonValue testPlanValue
jsonValue none
text getSubPath(50) = "niworkorder/v1/testplans/" + OrgDaten_SystemLink_TestPlanId.TXT
[testPlanValue, httpStatusCode] = sys:systemLink:request(systemLink:httpVerbs:GET, getSubPath, none)
if httpStatusCode != 200 then
sys:logInfo(sys:ui, "Get test plan failed with http status %i", httpStatusCode)
return
endif
// 2. Write test plan json to file
jsonSerializer serializer
serializer:writeToFile("/home/patools/systemlink/testplan.json", testPlanValue)
// 3. Evaluate test plans state property
jsonObject testPlan = testPlanValue:getValueAsJsonObject()
jsonValue state = testPlan:getValue("state")
text stateText(20) = state:getValueAsText()
if stateText != "IN_PROGRESS" then
sys:logInfo(sys:ui, "Unexpected test plan state: %s", stateText)
return
endif
// 4. Evaluate test plan properties
jsonValue propertiesValue = testPlan:getValue("properties")
jsonObject properties = propertiesValue:getValueAsJsonObject()
jsonMember m
foreach m in properties:getMembers()
text name(10) = m:getName()
if name == "StartTime" then
jsonValue startTime = m:getValue()
text startTimeText(30) = startTime:getValueAsText()
sys:logInfo(sys:ui, "Test plan start time: %s", startTimeText)
break
endif
endforeach
// 5. Add EndTime as property to the test plan
// 5.1 Edit the test plan
timestamp now = sys:calender:time
text timeText(30) = sprintf("%i/%i/%i %02i:%02i:%02i", now:month, now:day, now:year, now:hour, now:minute, now:second)
jsonValue time(timeText)
properties:addOrSetValue("EndTime", time)
// 5.2 Build the request body using edited test plan
jsonObject updateRequest
jsonArray testPlans
testPlans:addValue(testPlanValue)
jsonValue testPlansValue(testPlans)
updateRequest:addOrSetValue("testPlans", testPlansValue)
jsonValue updateRequestValue(updateRequest)
jsonValue updateTestPlanResponse
[updateTestPlanResponse, httpStatusCode] = sys:systemLink:request(systemLink:httpVerbs:POST, "niworkorder/v1/update-testplans", updateRequestValue)
// 6. Transition test plan state from "In progress" to "Pending approval"
jsonObject executeRequest
jsonValue end("END")
executeRequest:addOrSetValue("Action", end)
text executeSubPath(50) = "niworkorder/v1/testplans/" + OrgDaten_SystemLink_TestPlanId.TXT + "/execute"
jsonValue executeRequestValue(executeRequest)
jsonValue executeTestPlanResponse
[executeTestPlanResponse, httpStatusCode] = sys:systemLink:request(systemLink:httpVerbs:POST, executeSubPath, executeRequestValue)
endprogram
Beispiel für einen JSON-Testplan, der in 1–4 verwendet wird:
{
"id": "1000",
"name": "Battery Cycle Test",
"state": "IN_PROGRESS",
"properties":
{
"Location": "Austin",
"Cost Center": "12345"
}
}
Beispiel für eine JSON-Testplananfrage, die in 5 verwendet wird:
{
"testPlans": [
{
"id": "1000",
"name": "Battery Cycle Test",
"state": "IN_PROGRESS",
"properties": {
"Location": "Austin",
"Cost Center": "12345",
"EndTime": "3/24/2025 15:49:04"
}
}
]
}
Beispiel für eine JSON-Ausführung eines Testplans, die in 6 verwendet wird:
{
"action": "END"
}