Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.1K
Views
Cypress: extract value from JSON body to a variable

I am using cypress.io to test an API(Created using Node.js). I want to extract value from JSON response of the API and save it to a variable.

I have tried solutions mentioned in the below links it did not work: Cypress - get value from json response body

Below is the code i am using to test the API:

/// <reference types="cypress" />
describe("Testing Provider Catalog API", () => {
    it("Provider Catalog API GET Request", () => {
        cy.request('GET', 'v1/providers')
            .then((response) => {
                expect(response).to.have.property('status', 200)
                expect(response.body).to.not.be.null
                // expect(response.body.providers).to.have.length(1)
            })
        cy.screenshot()
    })
    it("Provider Catalog API POST Request", () => {
        const provider = {
            "type": "loadboard",
            "name": "123loadboard"
        };
        cy.request('POST', 'v1/providers', provider)
        cy.screenshot()
    })
    it("Provider Catalog API PUT Request", () => {
        const provider = {
            "type": "loadboard",
            "name": "123loadboard"
        };
        cy.request('PUT', 'v1/providers/61a54a66a2b734859481931c', provider)
        cy.screenshot()
    })
    it("Provider Catalog API DELETE Request", () => {
        cy.request('DELETE', 'v1/providers/61a68e7ca6011e605029191b')
        cy.screenshot()
    })
})

Below is the code that i am using

       var providerId
        cy.wait('@newObject').then((response) => {
            expect(response.status).to.eq(201)
            expect(response.responseBody).to.have.property('_id')
            const body = (response.responseBody)
            providerId = body['_id']
        })
        cy.get(someInput).type(newId)

Sample output of the API:

{
"_id":"61a54ba1a2b7348594819323",
"type":"loadboard",
"name":"123loadboard",
"__v":0
}

I want to store the value of the _id in a variable and use it later. I have been trying to for the last couple of days and nothing seems to work. Can anyone please help me. Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The recommended way to use variables with cypress is with aliases. See docs here.

In your code, you can wrap() your _id and save it as an alias, then call your alias somewhere else in your code:

cy.wait('@newObject').then((response) => {
    expect(response.status).to.eq(201)
    expect(response.responseBody).to.have.property('_id')
    cy.wrap(response.responseBody._id).as('newId')
})
// then later in your code use:
cy.get('@newId').then((newId) => {
    cy.get(someInput).type(newId)
})

You could also type() your _id inside your wait():

cy.wait('@newObject').then((response) => {
    expect(response.status).to.eq(201)
    expect(response.responseBody).to.have.property('_id')
    cy.get(someInput).type(response.responseBody._id)
})

Or you can use cypress global environmment object Cypress.env(). See docs here.

cy.wait('@newObject').then((response) => {
    expect(response.status).to.eq(201)
    expect(response.responseBody).to.have.property('_id')
    Cypress.env('newId', response.responseBody._id)
})
// then later in your code use:
cy.get(someInput).type(Cypress.env('newId'))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!