Manish B
Manish B

Reputation: 429

How to append a new key,value pair in json in cypress

I have one field which i want to read and append in existing json. I know cy.write({a+}) appends the data to json file but it creates a new pair of curly braces. I want to write an existing json in below format

{
  "Name":"X",
  "Age:,"Y",
  "New_data":"Z"
}

Right now the format looks like this

{
  "Name":"X",
  "Age:,"Y",
}
{
  "New_data":"Z"
}

Which is an elegant way to write data in existing json.

Upvotes: 3

Views: 3292

Answers (3)

Fody
Fody

Reputation: 32138

The answers already given are correct, and are the conventional and documented way to approach the problem.

You can also use a Proxy set() handler to automatically persist the object every time you add a property.

function persist(fileName, initial = {}) {
  Cypress._myObject = { ...initial }

  const handler = {
    set(obj, prop, value) {
      Reflect.set(...arguments)
      cy.writeFile(`./cypress/fixtures/${fileName}`, Cypress._myObject, { log: false });
      return true;
    }
  };
  
  return new Proxy(Cypress._myObject, handler);
}

// Wrap the object
const myObject = persist('myObject.json', { name: 'fred', age: 30 })

it('test1', () => {

  // Modify it, fixture file is also updated
  myObject.address1 = "somewhere"
  myObject.address2 = "somecity"   // could be set in a 2nd test

  // Check the fixture file
  cy.fixture('myObject.json').then(console.log)
    .should(fixture => {
      expect(fixture.address1).to.eq('somewhere')
      expect(fixture.address2).to.eq('somecity')
    })
})

Alternatively, a custom command (requires a nesting level)

/cypress/support/index.js

Cypress.Commands.add('persist', (fileName, initial = {}) => {
  Cypress._myObject = { ...initial }

  const handler = {
    set(obj, prop, value) {
      Reflect.set(...arguments)
      cy.writeFile(`./cypress/fixtures/${fileName}`, Cypress._myObject, { log: false });
      return true;
    }
  };
  
  return new Proxy(Cypress._myObject, handler)
})

test

it('test2', () => {

  cy.persist('myObject.json', { name: 'fred', age: 30 })
    .then(myObject => {

      myObject.address1 = "somewhere"
      myObject.address2 = "somecity"

      cy.fixture('myObject.json')
        .should(fixture => {
          expect(fixture.address1).to.eq('somewhere')
          expect(fixture.address2).to.eq('somecity')
        })
    })
})

Upvotes: 1

Krupal Vaghasiya
Krupal Vaghasiya

Reputation: 550

If you want to append data in a JSON file you need to use cy.readfile first Before writing JSON file:

cy.readFile('cypress/fixtures/example.json').then((data) => {
  data.newKey = 'newValue'
  cy.writeFile('cypress/fixtures/example.json', data)
})

Upvotes: 1

Alapan Das
Alapan Das

Reputation: 18601

Before JSON:

{
   "Name":"X",
   "Age":"Y"
}

Assuming you have a file example.json inside the fixtures folder. Your code should look like:

cy.readFile('cypress/fixtures/example.json').then((data) => {
  data.newKey = 'newValue'
  cy.writeFile('cypress/fixtures/example.json', JSON.stringify(data))
})

After JSON:

{
   "Name":"X",
   "Age":"Y",
   "newKey":"newValue"
}

Upvotes: 3

Related Questions