Home > Enterprise >  Delete id from json regex
Delete id from json regex

Time:07-15

I have a json array with thousands of elements with _id that I want to remove:

  [
   {
      "_id":{
         "$oid":"61d6633647ea79714d89fc9c"
      },
      "name":"test"
   },
   {
      "_id":{
         "$oid":"61d6633647ea79714d124124214"
      },
      "name":"test2"
   }
]

How can I delete this using a regex in notepad or js? I need to delete the comma aswell

e.g. For the example I need to delete "_id":{"$oid":"61d6633647ea79714d124124214"}, and "_id":{"$oid":"61d6633647ea79714d89fc9c"},

CodePudding user response:

You can just loop your array and delete item._id, then encode back to JSON string if needed

let jsonArray = [
   {
      "_id":{
         "$oid":"61d6633647ea79714d89fc9c"
      },
      "name":"test"
   },
   {
      "_id":{
         "$oid":"61d6633647ea79714d124124214"
      },
      "name":"test2"
   }
];

for (let item of jsonArray) {
    delete item._id;
}

console.log(JSON.stringify(jsonArray));

  • Related