简体   繁体   中英

Ignore Hyphen in Javascript (Postman)

Building a RestAPI with Postman.

I have some JSON data:

{
    "progress-update": {
        "@type": "parallel-progress",
        "job": {
            "@href": "/api/space/job-management/jobs/4691268"
        },
        "taskId": 4691268,
        "jobName": "Compare Config-4691268",
        "state": "DONE",
        "status": "SUCCESS",
        "percentage": 100,
        "data": "<![CDATA[Total requests: 3<br>InSync count : 3<br>OutOfSync count : 0<br>]]>",
        "subTask": [
            {

I want to pull the "state" value into an environment Variable that i can then use to determine wether to continue on to the next request or wait until the state is DONE.

The problem i'm running into is "progress-update": has a hyphen in it, causing my script to not recognize it.

var jsonData = JSON.parse(responseBody);
pm.environment.set("JobStatus", jsonData.progress-update.state);

Postman returns the following error:

There was an error in evaluating the test script: ReferenceError: update is not defined

You should be able to access your JSON data with

var jsonData = JSON.parse(responseBody);
pm.environment.set("JobStatus", jsonData['progress-update'].state);

using the object bracket notation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM