Add an extension to the JSON String object to make them unique

I have a JSON object that is passed to me as a String, but the object in its string form contains duplicate properties. I need to temporarily add extra numbers to properties in order to avoid a problem with duplicate JSON properties. As soon as I finish editing the object, I will JSON.Stringify return the object to String and delete the numbers.

Here is the line I passed:

{ "View":{ "Image":{ "BackgroundImage":"Image.png", "Position":[0,0], "Width":320, "Height":480 }, "Button":{ "BackgroundImage":"ButtonTop.png", "Position":[61,83], "Width":217, "Height":58 }, "Button":{ "BackgroundImage":"ButtonBottom.png", "Position":[61,214], "Width":205, "Height":73 }, "TextField":{ "BackgroundImage":"TextFieldLogin.png", "Position":[102,336], "Width":189, "Height":31 }, "Label":{ "Position":[137,100], "Width":72, "Height":20, "Text":"Hi Steve", "FontSize":18, "Color":[0,0,0,1] }, "Label":{ "Position":[43,342], "Width":54, "Height":20, "Text":"Login:", "FontSize":18, "Color":[0,0,0,1] }, "Label":{ "Position":[115,234], "Width":54, "Height":20, "Text":"Button", "FontSize":18, "Color":[0,0,0,1] } } } 

Here is how I would like the result to be:

 { "View_1":{ "Image_1":{ "BackgroundImage":"Image.png", "Position":[0,0], "Width":320, "Height":480 }, "Button_1":{ "BackgroundImage":"ButtonTop.png", "Position":[61,83], "Width":217, "Height":58 }, "Button_2":{ "BackgroundImage":"ButtonBottom.png", "Position":[61,214], "Width":205, "Height":73 }, "TextField_1":{ "BackgroundImage":"TextFieldLogin.png", "Position":[102,336], "Width":189, "Height":31 }, "Label_1":{ "Position":[137,100], "Width":72, "Height":20, "Text":"Hi Steve", "FontSize":18, "Color":[0,0,0,1] }, "Label_2":{ "Position":[43,342], "Width":54, "Height":20, "Text":"Login:", "FontSize":18, "Color":[0,0,0,1] }, "Label_3":{ "Position":[115,234], "Width":54, "Height":20, "Text":"Button", "FontSize":18, "Color":[0,0,0,1] } } } 

How can I use javascript.replace () to add numbering on demand and then remove numbering on demand?

+4
source share
2 answers

Like this? I agree with the other voices here, recommending that the one who supplies this "JSON" is responsible for providing the correct syntax, but by not allowing this, you can start:

 function formatJSON(input) { return input.replace(/"([^"]+?)":{(.+)}/g, function(string, key, value) { var dict = {}; return '"' + key + '":{' + value.replace(/"([^"]+?)":{(.+?)}/g, function(string, key, value) { dict[key] = dict[key] == undefined ? 1 : ++dict[key]; return '"' + key + '_' + dict[key] + '":{' + formatJSON(value) + '}'; }) + '}'; }); }; 

JSFiddle: http://jsfiddle.net/WYkAT/

Note that this will not result in renaming all keys in a more complex, deeper JSON string. It can be easily modified to be more recursive and less specific for your specific situation, but is likely to lead to some performance degradation. If you need a complete solution, I would like to modify the existing JSON.parse polyfill. Here are two (JSON2 and JSON3):

+1
source

You can use the RegEx expression and pass the function to the replace() method to generate a new name. Assuming json contains a string;

 var i = 0; json.replace(/\"Button\"/g, function(match) { return '"Button_' + i++ + '"'; }); 

For more information, see the Find and Replace with Style section of this article Regular Expressions in JavaScript, Part 2 .

EDIT:

To get an array of possible object names, use this:

 var names = json.match(/\"(\w*)\"\s?:\s?{/g) 

Then you can loop through the array to do all the replacements;

 for(n = 0; n < names.length; n++) { var i = 0; var name = names[n].replace(/\s?:\s?{/g,''); var re = new RegExp(name,'g'); json = json.replace(re, function(match) { return '"' + name.replace(/"/g,'') + '_' + i++ + '"'; }); } 

This time you need to create a RegEx object to insert a variable.

+1
source