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

292
Views
Javascript, replace "," with "." in JSON

I have as an input an invalid JSON string with this structure:

{
 "Seq_N":66,
 "Uptime":728,
 "Hum":33,500000,
 "Temp (C)":20,129999
}

I have no accesso to the code that produces the invalid JSON string so I can't fix the string before it's generated

In order to make the JSON string valid, I need to replace the comma character inside the Hum and Temp fields with a dot character, but I also need to keep the comma separators of the JSON string (in order not to break the JSON syntax)

Does anyone know a clean function/regex to do this?

Thanks in advance

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

You can use regex like this:

var json = `{
 "Seq_N":66,
 "Uptime":728,
 "Hum":33,500000,
 "Temp (C)":20,129999
}`;

json = json.replace(/("(Hum|Temp \(C\))":\d+),/g, '$1.');

console.log(json)
about 4 years ago · Santiago Gelvez Report

0

Negative lookahead would do the job.

const foo = '{ "Seq_N":66, "Uptime":728, "Hum":33,500000, "Temp (C)":20,129999 }';

const cleanedFoo = foo.replace(/,(?![" ])/g, ".");

console.log(JSON.parse(cleanedFoo));

The regex means:

, match comma,

(?! only if it's not followed

[" ] by either double quotes or space. (Yes, this so-called JSON is seriously malformed).

about 4 years ago · Santiago Gelvez Report

0

If I understand the question correctly, the following code should do the job:

$(".stat-item").each(function () {
var inner_context = $(this).text();
$(this).text(inner_context.replace(/[\.]+/g, ","));
});
about 4 years ago · Santiago Gelvez 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!