Solved

Replace colon characters in JSON fields

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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)
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…

821 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