How to format JSON file data by filtering only info needed?

Viewed 45

I recently exported all my user data from Firebase and now I want to format the JSON file to filter only the relevant field I need for my data model.

The file I got on Firebase is currently stored like this:

{
  "Users": {
    "00uniqueuserid3": {
      "1UserName": "Pusername",
      "2Password": "password",
      "3Email": "email@gmail.com",
      "4City": "dubai"
    }
  }
}

The issue is that the JSON file got over 5,000 users and I cannot get possibly manual format them how I want them. Is there any Javascript script or tool I can use to reformat all the data in the file, I would like to format them as such:

{"id":  uniqueid , "name": name, "email": email, "city": city}
1 Answers

You can create a new NodeJS project (npm init -y) and install mongodb. Then read the JSON file and modify the format using JS:

const mongodb = require("mongodb")
const exportedData = require("./myFirebaseExport.json");

const db = "" // <-- Mongo Client 

const data = Object.values(exportedData.Users);

const parsedData = data.map((d) => {
  // modify the data array as required; 
  return {
    name: d.username, // replace with your field names
    email: d.Email,
  }
})

await db.collection("users").insertMany(parsedData);
Related