Solved

Replace colon characters in JSON fields

Posted on 2015-01-27
5
437 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

831 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