Solved

Replace colon characters in JSON fields

Posted on 2015-01-27
5
498 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Regular Expression needed 4 38
Javascript question 8 39
Pass a js value to an html form 5 41
Switching ids and wanting to access array to output right value 6 15
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 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)

738 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