PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 20, 2022

[FIXED] how i can delete a authcode include json file javascript?

 October 20, 2022     javascript, json, node.js     No comments   

Issue

i have a question how i can delete a something include a json file ?

that's a example

my file name is test.json

this Before i delete auth code

{
  "auth": [
    {
      "test": 944037
    },
    {
      "tester": 261742
    }
  ]
}

i want for example delete a test

after i delete auth

{
  "auth": [
    {
      "tester": 261742
    }
  ]
}

My All database like that


Solution

Your problem is unclear, are you struggling with

  1. Reading the file?
  2. Parsing and manipulating the JSON?
  3. Saving the file?

Anyways, this code might help you:

const fs = require("fs");

fs.readFile("./test.json", (err, data) => {
  let json = JSON.parse(data.toString());

  // shift() removes the first object in 'auth'. Manipulate json however you wish.
  json["auth"].shift();

  // Remove second object (as asked by OP)
  json["auth"].pop();

  fs.writeFile("./test.json", JSON.stringify(json), function (err) {
    if (err) {
      return console.log(err);
    }
    console.log("The file was saved!");
  });
});

It first reads the file, parses the contents into JSON. Then manipulates the JSON, and then saves the file, and will console.log("The file was saved!"); if successful.



Answered By - BookOfCooks
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing