最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if JSON object exist (Postman) - Stack Overflow

programmeradmin1浏览0评论

I am trying to write a test in Postman to check if JSON keys are present in the response I received from the server.

The response:

{
  "Result": 0,
  "ResponseStatus": {
    "ErrorCode": null,
    "Message": null,
    "StackTrace": null,
    "Errors": null
  },
  "ResponseHeader": {
    "Succeeded": true,
    "Errors": null
  },
  "SessionId": "XXX-XXX-XXX"
}

I want to check "Results, Errorcode, Message,Succeeded" etc..

Thanks!

I am trying to write a test in Postman to check if JSON keys are present in the response I received from the server.

The response:

{
  "Result": 0,
  "ResponseStatus": {
    "ErrorCode": null,
    "Message": null,
    "StackTrace": null,
    "Errors": null
  },
  "ResponseHeader": {
    "Succeeded": true,
    "Errors": null
  },
  "SessionId": "XXX-XXX-XXX"
}

I want to check "Results, Errorcode, Message,Succeeded" etc..

Thanks!

Share Improve this question edited Oct 5, 2016 at 14:15 StephenTG 2,6576 gold badges28 silver badges37 bronze badges asked Oct 5, 2016 at 14:09 abovebeyond15abovebeyond15 1051 gold badge1 silver badge10 bronze badges 4
  • 1 What's your question? Where are you having trouble? – WillardSolutions Commented Oct 5, 2016 at 14:12
  • share your backend-api – Zahidur Rahman Commented Oct 5, 2016 at 14:12
  • @ZahidRahman — Why do you need to know details of the backend API in order to answer a question about writing a test against JSON that has already been successfully retrieved? – Quentin Commented Oct 5, 2016 at 14:18
  • 2 @Quentin - the "JSON value check" is checking for json values. e.x : '"Succeeded": true' - it will check if true is presented. i want to check if Succeeded is presented. i want to achieve a backend api test (so i will know when the schema has changed) – abovebeyond15 Commented Oct 6, 2016 at 7:44
Add a ment  | 

2 Answers 2

Reset to default 4

You could check the response scheme using:

var jsonData = JSON.parse(responseBody);
tests['response json contain Results'] = _.has(jsonData, 'Results');

According to your response body that you get, You can write simple test script for request under test section. You need to parse your json response first. The script will look like:

var jsonData = JSON.parse(responseBody);
tests["Succeeded with value true"] = jsonData.ResponseHeader.Succeeded === true;

similarly you can write tests for other checks.For sessionId I would suggest you to check it with sessionId where it gets generated(store it in environment and check with it in this request)

发布评论

评论列表(0)

  1. 暂无评论