Solved

Replace colon characters in JSON fields

Posted on 2015-01-27
5
381 Views
Last Modified: 2015-01-28
Hi All,

I would like to remove colons within JSON fields (properties) only and not the value parts.

Here is an example code:

var myJSON = {
  "path:t": "xlsx-file/xlsx_xml",
  "name:r": "xlsx_xml",
  "type:e": "folder"
}

Open in new window


Please could you provide me with the regexp pattern to use within a Javascript .replace() method which will identify colons (:) from within the property part only.

e.g. JSON.stringify(myJSON).toString(/regexp_pattern/g, '');

Thanks,

RIt
0
Comment
Question by:rito1
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40574841
why do you think it's possible to do with a unique regex?
http://jsfiddle.net/7pL81wth/1/
var myJSON = {
  "path:t": "xlsx-file/xlsx_xml",
  "name:r": "xlsx_xml",
  "type:e": "folder"
}
var keys = [];
for(var key in myJSON) keys[keys.length] = key;
var mystring = keys.toString();
// checking
alert(mystring);

Open in new window

0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40574852
You can use a regex with a callback function in this sort of situation. This will do the job:
var myString = JSON.stringify(myJSON).replace(/("(?:\\.|[^"])*")( *:)/g, function (m0, m1, m2) { return m1.replace(/:/g, '') + m2; });
alert(myString);

Open in new window

The regular expression contains some extra logic to try and not get confused by escaped quotes.
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 40574856
Hi,

You may create your own function that would iterate through the object's properties and create a new object:

var myJSON = {
  "path:t": "xlsx-file/xlsx_xml",
  "name:r": "xlsx_xml",
  "type:e": "folder"
}

var myStringify = function(obj){
  var result = {};
  for (var property in obj) {
      result[property.replace(/:/g, '')] = obj[property];
  }
  return JSON.stringify(result);
}

console.log(myStringify(myJSON));

Open in new window


This example is very simplistic meaning it only gets first level properties, but it could be a start.

A slightly modified version that would use recursion would be :

var myStringify = function(obj){
  var result = {};
  for (var property in obj) {
    if(typeof obj[property] === 'object'){
      obj[property] = myStringify(obj[property])
    }
    result[property.replace(/:/g, '')] = obj[property];
  }
  return result;
}
console.log(JSON.stringify(myJSON));
console.log(JSON.stringify(myStringify(myJSON)));

Open in new window


Still this is not perfect, as it doesn't take under consideration arrays.

Giannis
0
 
LVL 1

Author Closing Comment

by:rito1
ID: 40574866
Excellent this works well. Thank you.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40574882
1) My solution is more than two time faster than the accepted answer
2) Using a function as callback ? could you say it's still a regex way? No, because at this point, you don't really care about the regex itself...
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now