How to replace string values with numeric inside a json object used in google visualization api column chart
google-visualization, javascript, jquery, json, str-replace
Solution
Since JSON is effectively a string, why not put the entire string through a global `string.replace`:
jsonStr = JSON.stringify(jsonStr);
jsonStr = jsonStr.replace(/"v":"(\d+)"/g, '"v":$1');
Jsfiddle demo
Problem
I have this Json string that i use for google chart visualization that needs to be in this exact format and i need to replace every value of "v" that is a number to its numeric value( the value without the ""). I should do some javascript replace function, but i couldn't find a way to move around the json object. Here is and example json string that i should modify : ``` {"cols":[ {"id":"r","label":"Reason","type":"string"}, {"id":"m","label":"Minutes","type":"number"} ], "rows":[ {"c":[ {"v":"Flour - Blower","f":"Flour - Blower"}, {"v":"7","f":"7"}]}, {"c":[ {"v":"Whole Line - d","f":"Whole Line - d"}, {"v":"4","f":"4"}]}, {"c":[ {"v":"Flour - Pipework","f":"Flour - Pipework"}, {"v":"3","f":"3"}]}, {"c":[ {"v":"Horseshoe - Belt","f":"Horseshoe - Belt"}, {"v":"1","f":"1"}]} ], "p":null } ``` probably i should do something like : ``` var jsonStr = ...; for (i in jsonStr.rows) { for(j in jsonStr[i].c) { if (parseInt(jsonStr[i].c[j].v) != 'NaN') { jsonStr.rows[i].c[j].v = parseInt(jsonStr.rows[i].c[j].v); } } ```