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

159
Views
json.stringify object and rename variables in json

I want to serialize an object o, which has a method called, let's say, a. The object also holds a variable, which name is _a.

I now want to parse this object to a JSON string. But the JSON looks something like this:

{
    "_a": "",
    ...
}

Question

Is there a way, to comfortably remove/ replace the _ character(s) (or any character(s)).

What I have tried

  1. The rename parameter of the JSON.stringify() method.
    1.1. Didn't work, because you can only return altered values and no keys.
  2. Iterating all keys of an object, deleting them and creating a new renamed key, and assigning the value (see code below).
    2.1. This works, but is not really readable and nasty, when having "sub-object".
Object.keys(o).forEach(key => {
    Object.defineProperty(o, key.replace("_", ""),
        Object.getOwnPropertyDescriptor(o, key));
    delete o[key];
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Would something like this work for you:

let obj = {
  "_a": "",
  "b": "Test"
}

let result = Object.entries(obj).reduce((acc, [key, value]) => {
  let newKey = key.replace('_', '')
  acc[newKey] = value
  return acc
}, {})

console.log(result)

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!