I have some client data that sometines returns a json array and sometimes a single result.
tried:
var json = JSON.parse(data);
if(Array.isArray(data)){
console.log ("is array");
//loop
..
//end loop
} else {
console.log ("isn't array");
//process
}
But haven't got it working. Even a single json result is being detected as array.
In js, how do I work with it properly?
json looks like:
{"item":{"clave":"CEL-37","codigo_fabricante":"A2554181"}}
and
{"item":[{"clave":"AC-2972","codigo_fabricante":"EBP-2-003"},{"clave":"SWS-1994","codigo_fabricante":"TMBD-044"}]}
[]. You can check length of array and if it equals 1, then you're good to go :)itemproperty. If you want this to distinguish is that array or not, and you are always sure that you will have thisitemproperty, you can testif(Array.isArray(json.item))