Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

javascript - Checking a value in a nested JSON using Postman

I have a nested JSON returned from an API that I am hitting using a GET request, in POSTMAN chrome app. My JSON looks like this

"result": [
{

  "_id": "some_id",
  "name": "India",
  "code": "IN",
  "link": "http://www.india.info/",
  "closingTime": "2017-02-25T01:12:17.860Z",
  "openingTime": "2017-02-25T06:12:17.205Z",
  "image": "image_link",
  "status": "online",
  "serverStatus": "online",
  "games": [
    {
      "_id": "some_game_id1",
      "name": "Cricket"
    },
    {
      "_id": "some_another_id1",
      "name": "Baseball"
    },
    {
      "_id": "some_another_id_2",
      "name": "Basketball"
    }
  ]
},
{
  "_id": "some_id",
  "name": "Australia",
  "code": "AUS",
  "link": "https://www.lonelyplanet.com/aus/adelaide",
  "closingTime": "2017-02-28T05:13:38.022Z",
  "openingTime": "2017-02-28T05:13:38.682Z",
  "image": "some_image_url",
  "status": "offline",
  "serverStatus": "online",
  "games": [
    {
      "_id": "some_game_id_2",
      "name": "Cricket"
    },
    {
      "_id": "some_another_id_3",
      "name": "Kho-Kho"
    },
    {
      "_id": "some_another_id_4",
      "name": "Badminton"
    },
    {
      "_id": "some_another_id_5",
      "name": "Tennis"
    }
  ]
},

I am trying to test whether my response body has "name":"India" and the "game" with "some_game_id1" contains the "name":"cricket".

I went through this link where the answer is to have an array for "name"created and then check within the array whether the array contains the value. I tried this but my code fails.

Also, I tried searching the element by the index within the JSON body using this -

   var searchJSON = JSON.parse(responseBody);
   tests["name contains India"] = searchJSON.result.name[0]==="India";

But this also fails. I tried using the .value appended with the second line of above code, but it also fails. How can I check this thing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to put [0] after result (which is an array) rather than name (which is a string).

Also, use a regular expression to check whether the name contains 'India', because using === only checks if the name is exactly India.

var searchJSON = JSON.parse(responseBody)
tests["name contains India"] = /India/.test(searchJSON.result[0].name)


Demo Snippet:

var responseBody = `{
  "result": [{
      "_id": "some_id",
      "name": "India",
      "code": "IN",
      "link": "http://www.india.info/",
      "closingTime": "2017-02-25T01:12:17.860Z",
      "openingTime": "2017-02-25T06:12:17.205Z",
      "image": "image_link",
      "status": "online",
      "serverStatus": "online",
      "games": [{
          "_id": "some_game_id1",
          "name": "Cricket"
        },
        {
          "_id": "some_another_id1",
          "name": "Baseball"
        },
        {
          "_id": "some_another_id_2",
          "name": "Basketball"
        }
      ]
    },
    {
      "_id": "some_id",
      "name": "Australia",
      "code": "AUS",
      "link": "https://www.lonelyplanet.com/aus/adelaide",
      "closingTime": "2017-02-28T05:13:38.022Z",
      "openingTime": "2017-02-28T05:13:38.682Z",
      "image": "some_image_url",
      "status": "offline",
      "serverStatus": "online",
      "games": [{
          "_id": "some_game_id_2",
          "name": "Cricket"
        },
        {
          "_id": "some_another_id_3",
          "name": "Kho-Kho"
        },
        {
          "_id": "some_another_id_4",
          "name": "Badminton"
        },
        {
          "_id": "some_another_id_5",
          "name": "Tennis"
        }
      ]
    }
  ]
}`

var tests = {}

var searchJSON = JSON.parse(responseBody)
tests["name contains India"] = /India/.test(searchJSON.result[0].name)

console.log(tests) //=> { "name contains India": true }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...