I have a Json data like this
{
"info":[
{
"Name":"Noob Here",
"Major":"Language",
"Sex":"Male",
"English":"15",
"Japanese":"5",
"Calculus":"0",
"Geometry":"20"
},
{
"Name":"Noob Here",
"Major":"Calculus",
"Sex":"Female",
"English":"0.5",
"Japanese":"40",
"Calculus":"20",
"Geometry":"05"
}
]
}
I need to get only the key value pair where the value is numerical data.
Expected output
{
"English":"15",
"Japanese":"5",
"Calculus":"0",
"Geometry":"20"
},
{
"English":"0.5",
"Japanese":"40",
"Calculus":"20",
"Geometry":"05"
}
Is there any plugins for that or should i write long jquery code for it using regex?.
I have a Json data like this
{
"info":[
{
"Name":"Noob Here",
"Major":"Language",
"Sex":"Male",
"English":"15",
"Japanese":"5",
"Calculus":"0",
"Geometry":"20"
},
{
"Name":"Noob Here",
"Major":"Calculus",
"Sex":"Female",
"English":"0.5",
"Japanese":"40",
"Calculus":"20",
"Geometry":"05"
}
]
}
I need to get only the key value pair where the value is numerical data.
Expected output
{
"English":"15",
"Japanese":"5",
"Calculus":"0",
"Geometry":"20"
},
{
"English":"0.5",
"Japanese":"40",
"Calculus":"20",
"Geometry":"05"
}
Is there any plugins for that or should i write long jquery code for it using regex?.
Share Improve this question edited Mar 21, 2013 at 6:40 Okky asked Mar 21, 2013 at 6:31 OkkyOkky 10.5k15 gold badges77 silver badges123 bronze badges 8- How did you generate this json? can you put that code? – Sharun Commented Mar 21, 2013 at 6:39
- its just json file hardcoded. – Okky Commented Mar 21, 2013 at 6:39
- I dont think there is any plugins for that. You will have to loop though the json list and remove unwanted items – Sharun Commented Mar 21, 2013 at 6:44
-
BTW, JSON can store numbers natively, you don't have to store them as strings, e.g.
"English": 0.5
(no quotes around the number). If you are able to do this yourself in your JSON file, you can just usetypeof value === 'number'
to identify them in your JavaScript. – jokeyrhyme Commented Mar 21, 2013 at 6:47 - you dont need to put quotes for numbers. you can simply write them without quotes. and this does not seem to be valid json format. – Sangeeta Commented Mar 21, 2013 at 6:55
5 Answers
Reset to default 3used jquery isNumeric
to find if numeric or not.. then playing with the objects and array..you need two loops to get the objects...
here you go..
var finalObj={}; //this is final output object
var info = []; //array to push to finalObj
$.each(data.info,function(i,v){
var object={};
$.each(v,function(i1,val1){
if($.isNumeric(val1)){
object[i1]=val1
}
});
info.push(object);
});
finalObj['info']=info;
console.log(finalObj);
fiddle here
You can write a helper function. Just pass in your Json object and loop through each of the fields. The regex is straight forward.
var regex = new RegExp("\d*\.?\d");
var result = regex.test("yourvalue");
If result is true, it is a number. pile your results and return a Json object.
Try this:
var data={
"info":[
{
"Name":"Noob Here",
"Major":"Language",
"Sex":"Male",
"English":"15",
"Japanese":"5",
"Calculus":"0",
"Geometry":"20"
},
{
"Name":"Noob Here",
"Major":"Calculus",
"Sex":"Female",
"English":"0.5",
"Japanese":"40",
"Calculus":"20",
"Geometry":"05"
}
]
};
var newarr=[];
$(data.info).each(function(index,value){
arr=new Object();
$(Object.keys(value)).each(function(ind,val){
if($.isNumeric(value[val]))
{
arr[val]=value[val];
}
});
newarr.push(arr);
});
console.log(newarr);
Fiddle http://jsfiddle/RyVds/2/
To check Object.keys()
Read http://msdn.microsoft./en-us/library/ie/ff688127%28v=vs.94%29.aspx and https://developer.mozilla/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys
you can parse the value with parseInt
and if it is integer than select it.
test is equal to the json object to posted.
var obj = test.info[0];
for (var key in obj) {
if(!isNaN(parseInt(obj[key]))){
alert(JSON.stringify(key));
}
}
http://jsfiddle/btevfik/gQfAN/
You can loop through the keys on each object and try parseInt the value. It will return NaN if it's a string.
for (var i = 0; i < obj.info.length; i++) {
var tmp = {};
keys = Object.keys(obj.info[i]);
console.log(keys);
for (var j = 0; j < keys.length; j++) {
if (parseInt(obj.info[i][keys[j]])) {
tmp[keys[j]] = obj.info[i][keys[j]];
}
}
Full code is on a jsfiddle:
http://jsfiddle/wXBKd/1/