Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

How to validate array in json response?

Greatings,

I was wondering how to validate an array with postman

I have this answer:

{
"documents": [
            {
                "documentType": "FORM",
                "attributes": [
                    {
                        "kind": {
                            "code": "COMPANY",
                            "name": "Company name"
                        },
                        "value": "KKO",
                        "confidence": 0.0
                    },
                    {
                        "kind": {
                            "code": "AGREEMENT",
                            "name": "Agreement number"
                        },
                        "value": "123",
                        "confidence": 0.0
                    },
                    {
                        "kind": {
                            "code": "DATE_OF_ISSUE",
                            "name": "When it was created"
                        },
                        "value": "01/01/2019",
                        "confidence": 0.0
                    }
}

I want to validate the second attribute which is:

{
                        "kind": {
                            "code": "AGREEMENT",
                            "name": "Agreement number"
                        },

Including value field

                        "value": "123",
                        "confidence": 0.0
                    },

I was wondering what will be the easiest way to validate this?

I've tried to write paths like:

const response = pm.response.json()

attribute1 = response.data.documents[0];
attribute2 = response.data.documents[1];
attribute3 = response.data.documents[3];

pm.expect(attributes[1].kind.code).to.equal("AGREEMENT");

pm.expect(attributes[1].kind.name).to.equal("Agreement number");

but it is all too heavy and time consuming.

I would much appreciate if you share with me your experience and provide the clue on how to resolve my issue.

Thank you in advance

question from:https://stackoverflow.com/questions/65897829/how-to-validate-array-in-json-response

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

1 Reply

0 votes
by (71.8m points)
pm.expect([1,2,3,4,5]).to.deep.equal([1,2,3,4,5])

or

pm.expect(_.isEqual([1,2,3,4,5],[1,2,3,4,5])).to.be.true

so in your case:

  pm.expect([
    {
        "kind": {
            "code": "COMPANY",
            "name": "Company name"
        },
        "value": "KKO",
        "confidence": 0.0
    },
    {
        "kind": {
            "code": "AGREEMENT",
            "name": "Agreement number"
        },
        "value": "123",
        "confidence": 0.0
    },
    {
        "kind": {
            "code": "DATE_OF_ISSUE",
            "name": "When it was created"
        },
        "value": "01/01/2019",
        "confidence": 0.0
    }]).to.deep.equal([
                    {
                        "kind": {
                            "code": "COMPANY",
                            "name": "Company name"
                        },
                        "value": "KKO",
                        "confidence": 0.0
                    },
                    {
                        "kind": {
                            "code": "AGREEMENT",
                            "name": "Agreement number"
                        },
                        "value": "123",
                        "confidence": 0.0
                    },
                    {
                        "kind": {
                            "code": "DATE_OF_ISSUE",
                            "name": "When it was created"
                        },
                        "value": "01/01/2019",
                        "confidence": 0.0
                    }])

Update

name = ["Company name", "Agreement number", "When it was created"]
value = ["KKO", "123", "01/01/2019"]

pm.test("something", function () {
    a.forEach((a, i) => {
        pm.expect(a.kind.name).to.be.equal(name[i])

        pm.expect(a.value).to.be.equal(value[i])
    })
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...