3

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

Sean Liles
  • 55
  • 2
  • 8
  • 2
    Try using `jsonData['progress-update'].state` instead – junwen-k Nov 16 '19 at 17:26
  • Does this answer your question? [How do I reference a javascript object property with a hyphen in it?](https://stackoverflow.com/questions/7122609/how-do-i-reference-a-javascript-object-property-with-a-hyphen-in-it) – Henke Jan 29 '21 at 15:16

1 Answers1

4

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

junwen-k
  • 3,454
  • 1
  • 15
  • 28
  • Thanks a lot. I knew it would be something simple like that. – Sean Liles Nov 16 '19 at 17:47
  • 1
    I would also suggest using `pm.response.json() ` over the older syntax of `JSON.parse(responseBody)`. Would make it consistent with the new `pm.environment.set()` you're using. – Danny Dainton Nov 16 '19 at 18:52
  • Can someone please explain why, or how, wrapping it in array brackets and quotes works? (And perhaps why escaping with backslash does not?) – Kreidol Feb 03 '23 at 23:20