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

180
Views
Remove data from string property in a json

Given the following log object:

{
  "message": "login: error {\"error\":{\"message\":\"Network Error\",\"name\":\"Error\",\"stack\":\"Error: Network Error\\n    at something (somewhere)\\n    at something (somewhere)\",\"config\":{\"url\":\"/a/place\",\"method\":\"get\",\"headers\":{\"Accept\":\"application/json, text/plain, */*\",\"Authorization\":\"bla blablabla\",\"X-Amzn-Trace-Id\":\"yadiyadiyadi\"},\"baseURL\":\"verygoodplace"}}}",
  "level": "warning",
  "sessionId": "blablabla"
}

How can I remove the message.headers.Authorization entry completely? Since it appears inside a string, I can't (directly) use lodash unset, and I somehow need to alter the string.

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

0

I would recommend to clean message a bit so it can be parsed with JSON.parse(). It looks like discarding text before the first { will be all that's needed.

Parsing will create a JS object that is easy to manipulate, after which you can use JSON.stringify() to convert it back to a similar string as what you started with.

It may seem like a lot of steps, but doing this kind of string manipulation directly could be an even bigger pain in the you-know-where.

Working demo:

const logObj = {
  "message": "login: error {\"error\":{\"message\":\"Network Error\",\"name\":\"Error\",\"stack\":\"Error: Network Error\\n    at something (somewhere)\\n    at something (somewhere)\",\"config\":{\"url\":\"/a/place\",\"method\":\"get\",\"headers\":{\"Accept\":\"application/json, text/plain, */*\",\"Authorization\":\"bla blablabla\",\"X-Amzn-Trace-Id\":\"yadiyadiyadi\"},\"baseURL\":\"verygoodplace\"}}}",
  "level": "warning",
  "sessionId": "blablabla"
}

const index = logObj.message.indexOf("{");
const jsonText = logObj.message.substring(index);
const parsed = JSON.parse(jsonText);

delete parsed.error.config.headers.Authorization;   // remove unwanted node

console.log("The cleaned message:");
console.log(JSON.stringify(parsed, undefined, 2));  // print with indentation

const prefix = logObj.message.substring(0, index);
logObj.message = prefix + JSON.stringify(parsed);

console.log("The updated logObj:");
console.log(JSON.stringify(logObj, undefined, 2));  // print with indentation

Note - to make this work I had to change verygoodplace" to verygoodplace\", it looks like you made an error when preparing the log object for use in the question.

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!