8

I have JSON stringify data like this :

[{"availability_id":"109465","date":"2017-02-21","price":"430000"},{"availability_id":"109466","date":"2017-02-22","price":"430000"},{"availability_id":"109467","date":"2017-02-23","price":"430000"}]

I want to get only price value of that data. I have tried this way but it doesn't work.

var stringify = JSON.stringify(values);

for(var i = 0; i < stringify.length; i++)
{
    alert(stringify[i]['price']);
}

How could I to do that ?

Yaman Jain
  • 1,254
  • 11
  • 16
Antonio
  • 755
  • 4
  • 14
  • 35

4 Answers4

21

This code will only fetch the price details.

var obj = '[{"availability_id":"109465","date":"2017-02-21","price":"430000"},{"availability_id":"109466","date":"2017-02-22","price":"430000"},{"availability_id":"109467","date":"2017-02-23","price":"430000"}]';
var stringify = JSON.parse(obj);
for (var i = 0; i < stringify.length; i++) {
    console.log(stringify[i]['price']);
}
Yaman Jain
  • 1,254
  • 11
  • 16
  • 1
    it's necessary to note that the variables in this code snippet are named badly (the op fault). I would change it as: `obj` -> `jsonString` (or `dataString`), `stringify` -> `jsonObj` (or `parsedData`, or simply `data`) – MortezaE Sep 14 '20 at 15:06
10

Observation :

If you want to parse the array of objects to get the property value you have to convert in into JSON object first.

DEMO

var jsonStringify = '[{"availability_id":"109465","date":"2017-02-21","price":"430000"},{"availability_id":"109466","date":"2017-02-22","price":"430000"},{"availability_id":"109467","date":"2017-02-23","price":"430000"}]';

var jsonObj = JSON.parse(jsonStringify);

for(var i = 0; i < jsonObj.length; i++)
{
    alert(jsonObj[i]['price']);
}
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
2

you will geting a stringified object like this

var obj='[{"availability_id":"109465","date":"2017-02-21","price":"430000"},
{"availability_id":"109466","date":"2017-02-22","price":"430000"},
{"availability_id":"109467","date":"2017-02-23","price":"430000"}]';

parse your obj using JSON.parse(object) then apply this loop ad let me know it get any error lie this

var parseObject = JSON.parse(object);
Asad
  • 3,070
  • 7
  • 23
  • 61
0

instead of using stringify before selecting the data you should use your loop directly on the values array.

For example :

var priceArray = array();
values.forEach (data) {
    alert(data['price'];
    priceArray.push(data['price']);
}

stringify = JSON.stringify(values);
stringifiedPriceArray = JsON.stringify(priceArray);

Once stringified, you can't reach the data in your array

Sylvain
  • 417
  • 7
  • 16