2

I have the below output from Postman or hitting end point(we can say).

{
  "SearchResult": {
    "total": 11,
    "resources": [
      {
        "id": "12345",
        "name": "GuestType",
        "description": "Identity group ",
      },
      {
        "id": "56789",
        "name": "Admin",
        "description": "",
      },
    ]
  }
}

I want to extract "id" and "name" from these values. I see the values are inside sub-blocks. How to extract these key-value using java-script that need to be put in "Tests" tab in postman?

4

3 回答 3

1

var obj={
  "SearchResult": {
    "total": 11,
    "resources": [
      {
        "id": "12345",
        "name": "GuestType",
        "description": "Identity group ",
      },
      {
        "id": "56789",
        "name": "Admin",
        "description": "",
      },
    ]
  }
}


obj.SearchResult.resources.forEach((o)=>console.log(o.id,o.name));

于 2019-11-18T11:02:41.840 回答
1

Below code will return array of objects with id and name only.... Happy coding :)

let data = {
  "SearchResult":
  {
    "total": 11,
    "resources": [
      { "id": "12345", "name": "GuestType", "description": "Identity group ", },
      { "id": "56789", "name": "Admin", "description": "", }
    ]
  }
}

let ids = data.SearchResult.resources.map(obj => {
  id: obj.id,
  name: obj.name
});
于 2019-11-18T11:15:49.833 回答
0

Try

let ids = data.SearchResult.resources.map(obj => obj.id);
let names = data.SearchResult.resources.map(obj => obj.name);

let data={ "SearchResult": { "total": 11, "resources": [ { "id": "12345", "name": "GuestType", "description": "Identity group ", }, { "id": "56789", "name": "Admin", "description": "", }, ] } }

let ids = data.SearchResult.resources.map(obj => obj.id);
let names = data.SearchResult.resources.map(obj => obj.name);

console.log(ids);
console.log(names);

于 2019-11-18T10:57:38.700 回答