2

I am new to JSON and Postman. I believe I'm trying to do something very simple.

I have created a GET request which will get a JSON response like the one below.

In the example below I want to get the value of the last "IsArchived" attribute (the one just before the "Version"); Or the one that does not belong to a FieldGroup.

How can I do it? (although maybe it will be answered by an answer to my other question) Thanks in advance

{
    "Id": 1328,
    "Name": "AAA Test",
    "Owner": {
        "Id": 208,
        "Name": "The Boss"
    },
    "FieldGroups": [
        {
            "Id": "c81376f0-6ac3-4028-8d61-76a0f815dbf8",
            "Name": "General",
            "FieldDefinitions": [
                {
                    "Id": 1,
                    "DisplayName": "Product Name",
                    "IsArchived": false
                },
                {
                    "Id": 2,
                    "DisplayName": "Short Description",
                    "IsArchived": false
                },
                {
                    "Id": 33,
                    "DisplayName": "Long Description",
                    "IsArchived": false
                },
            ]
        },
        {
            "Id": "5ed8746b-0fa8-4022-8216-ad3af17db91f",
            "Name": "Somethingelse",
            "FieldDefinitions": [
                {
                    "Id": 123,
                     "DisplayName": "Attribution",
                    "IsArchived": false
                },
                {
                    "Id": 1584,
                    "DisplayName": "FC1",
                    "IsArchived": false
                },
                {
                    "Id": 623,
                    "DisplayName": "Sizes",
                    "IsArchived": false,
                    "Owner": {
                        "Id": 208,
                        "Name": "The Boss"
                    },
                    "Unit": "",
                    "Options": [
                        {
                            "Id": 1,
                            "Value": "XS"
                        },
                        {
                            "Id": 2,
                            "Value": "S"
                        },
                        {
                            "Id": 3,
                            "Value": "M"
                        },
                    ]
                }
             ]
        }
    ],
    },
    "IsArchived": false
    "Version": 1
}
KVN
  • 863
  • 1
  • 17
  • 35
  • Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – Henke Feb 10 '21 at 16:59

2 Answers2

2

This should be rather easy because your IsArchived attribute is on the top level of the JSON response, so it will not happen that another IsArchived value is used unless you somehow loop through your FieldGroups array like in your other question.

// Convert the response body to a JSON object
var jsonData = pm.response.json()

// Create a variable and assign the value of IsArchived to it
var isArchived = jsonData.IsArchived;

// OR create a Postman environment variable and assign the value of IsArchived to it
pm.environment.set("isArchived", jsonData.IsArchived);
0xFEFFEFE
  • 120
  • 10
0

First of all, the JSON you're dealing with is not valid. You can always check your JSON validity with JSONLint.

Here is the valid JSON:

{
    "Id": 1328,
    "Name": "AAA Test",
    "Owner": {
        "Id": 208,
        "Name": "The Boss"
    },
    "FieldGroups": [{
            "Id": "c81376f0-6ac3-4028-8d61-76a0f815dbf8",
            "Name": "General",
            "FieldDefinitions": [{
                    "Id": 1,
                    "DisplayName": "Product Name",
                    "IsArchived": false
                },
                {
                    "Id": 2,
                    "DisplayName": "Short Description",
                    "IsArchived": false
                },
                {
                    "Id": 33,
                    "DisplayName": "Long Description",
                    "IsArchived": false
                }
            ]
        },
        {
            "Id": "5ed8746b-0fa8-4022-8216-ad3af17db91f",
            "Name": "Somethingelse",
            "FieldDefinitions": [{
                    "Id": 123,
                    "DisplayName": "Attribution",
                    "IsArchived": false
                },
                {
                    "Id": 1584,
                    "DisplayName": "FC1",
                    "IsArchived": false
                },
                {
                    "Id": 623,
                    "DisplayName": "Sizes",
                    "IsArchived": false,
                    "Owner": {
                        "Id": 208,
                        "Name": "The Boss"
                    },
                    "Unit": "",
                    "Options": [{
                            "Id": 1,
                            "Value": "XS"
                        },
                        {
                            "Id": 2,
                            "Value": "S"
                        },
                        {
                            "Id": 3,
                            "Value": "M"
                        }
                    ]
                }
            ]
        }
    ],
    "IsArchived": false,
    "Version": 1
}

Now I assume that you want to get attribute in Postman test, you can achieve as here:

var responseData = pm.response.json();
var isArchived = responseData.isArchived;
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
  • Thanks Div. Your answer works, just need to use ".IsArchived" instead of ".isArchived". – KVN Apr 02 '19 at 15:04
  • btw, why my json is not valid? I do not see the difference between yours and mine. – KVN Apr 02 '19 at 15:11
  • @KVN: copy your JSON, goto https://jsonlint.com/ paste there and click on validate button, they will tell you what's wrong – Divyang Desai Apr 02 '19 at 15:38
  • @KVN You had some `,` where they were not needed: after the last object in your arrays. – 0xFEFFEFE Apr 02 '19 at 15:45
  • 1
    Ahh got it. Thanks Div! – KVN Apr 02 '19 at 16:26
  • @Div do you know, by any chances, the answer to my other question (on the Count of All "IsArchived" in the response? https://stackoverflow.com/questions/55463615/postman-how-to-get-a-count-from-a-json-response-for-a-specific-attributes – KVN Apr 02 '19 at 16:29