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

133
Views
How do I change how a POST request is saved in my db.json?

I've created a fake JSON server using npm i json-server in my React application. I've created a db.json file and have created the following function to push a new 'task' to the server for persistence:

const addTask = async task => {
  const res = await fetch(`http://localhost:5000/tasks`, {
    method: 'POST',
    headers: {
      'Content-type': 'application/json'
    },
    body: JSON.stringify(task)
  })

  const data = await res.json()

  setTasks([...tasks, data])
}

Now, this is how my db.json is being saved:

{
  "tasks": [
    {
      "id": 1,
      "text": "Doctors Appointment",
      "day": "Feb 5th at 2:30PM",
      "reminder": true
    },
    {
      "text": "SWE Interview",
      "day": "May 9th at 9:30AM",
      "reminder": false,
      "id": 2
    },
    {
      "text": "School Meeting",
      "day": "Feb 11th at 5:30PM",
      "reminder": false,
      "id": 3
    }
  ]
}

id:1 has been manually entered and id:2 and id:3 have been POST'd in using the UI. How can I make it so that they follow the same pattern of arrangement as id:1 i.e. id, text, day then reminder?

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

0

Order of keys in JSON should not have any affect on the data. If you still need the object to be in a specific order, I would write custom function to stringify the JSON. Here's the code I came up with:

function stringifyTask(task) {
    return `{
    "id": ${task.id},
    "text": "${task.text}",
    "day": "${task.day}",
    "reminder": ${task.reminder}
}`
}
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!