-3

I have a json array like this

AlreadySaved:[  
{  
  "SNO":1,
  "SNUMBER":"827",
  "STARTDATE":"11/12/2016",
  "STARTTIME":"03:06:50 PM",
  "ITEMNAME":"KBand",
  "ITEMCODE":"KB2541",
  "ITEMSKUNAME":"Band",
  "ITEMSKUCODE":"BT102",
  "SALESRATE":100.0,
  "PURCHASERATE":5.0,
  "DOE":"~",
  "STOCKQTY":2.0,
  "PHYSICALQTY":1.0
}
]

I need to fetch value of PHYSICALQTY and display it in alert.I am new to jquery/javascript so any help would be appreciated.

GeekAb
  • 505
  • 4
  • 15
Shreya Rawal
  • 182
  • 3
  • 14
  • add what you have tried so far to achieve what you want and include in OP where you got stuck. Also your sample is not valid – guradio Dec 23 '16 at 07:42
  • 1
    Possible duplicate of [Parse JSON in JavaScript?](http://stackoverflow.com/questions/4935632/parse-json-in-javascript) – AMAN77 Dec 23 '16 at 08:31

7 Answers7

3

You can access json like normal objects in js via dot notation.

var json = {"STOCKQTY":2.0,"PHYSICALQTY":1.0};

console.log(json.PHYSICALQTY);

If you Have saved this json under another object You need to go deeper, eg.

var response = {AlreadySaved: [{"STOCKQTY":2.0,"PHYSICALQTY":1.0}] };

console.log(response.AlreadySaved[0].PHYSICALQTY);

Please remember also that in some cases You may have json (eg from response) as string not object.

console.log(typeof someJson); //string

In that cases You need to parse this string into json

var json = JSON.parse(someJsonAsString);

Happy hacking!

Hakier
  • 505
  • 2
  • 13
1

You can use json[0].PHYSICALQTY

var json = [{
  "SNO": 1,
  "SNUMBER": "827",
  "STARTDATE": "11/12/2016",
  "STARTTIME": "03:06:50 PM",
  "ITEMNAME": "KBand",
  "ITEMCODE": "KB2541",
  "ITEMSKUNAME": "Band",
  "ITEMSKUCODE": "BT102",
  "SALESRATE": 100.0,
  "PURCHASERATE": 5.0,
  "DOE": "~",
  "STOCKQTY": 2.0,
  "PHYSICALQTY": 1.0
}];

document.getElementById('id1').innerHTML=json[0].PHYSICALQTY;
<div>
PHYSICALQTY:<label id="id1"></label>
</div>
Naghaveer R
  • 2,890
  • 4
  • 30
  • 52
0

It is not json object.

Json:

{AlreadySaved:[{"SNO":1,"SNUMBER":"827","STARTDATE":"11/12/2016","STARTTIME":"03:06:50 PM","ITEMNAME":"KBand","ITEMCODE":"KB2541","ITEMSKUNAME":"Band","ITEMSKUCODE":"BT102","SALESRATE":100.0,"PURCHASERATE":5.0,"DOE":"~","STOCKQTY":2.0,"PHYSICALQTY":1.0}]}

In this case you may get your property: alert(AlreadySaved[0].PHYSICALQTY);

guradio
  • 15,524
  • 4
  • 36
  • 57
heoLixfy
  • 121
  • 1
  • 2
  • 11
  • what you call Json is not valid either - property names need to be in `""` - so, your example SHOULD be `{"AlreadySaved":[ ...` – Jaromanda X Dec 23 '16 at 07:49
0

You can parse this and then get the value. Ouf course if you receive this like a string.

var a = '[{"SNO":1,"SNUMBER":"827","STARTDATE":"11/12/2016","STARTTIME":"03:06:50 PM","ITEMNAME":"KBand","ITEMCODE":"KB2541","ITEMSKUNAME":"Band","ITEMSKUCODE":"BT102","SALESRATE":100.0,"PURCHASERATE":5.0,"DOE":"~","STOCKQTY":2.0,"PHYSICALQTY":1.0}]'

var output = JSON.parse(a)

console.log(output[0].PHYSICALQTY)
pwnz22
  • 469
  • 2
  • 9
  • 19
0

Your JSON object is not valid, you can check that at http://jsonlint.com/

Once you set it to something like

var AlreadySaved =[{"SNO":1,"SNUMBER":"827","STARTDATE":"11/12/2016","STARTTIME":"03:06:50 PM","ITEMNAME":"KBand","ITEMCODE":"KB2541","ITEMSKUNAME":"Band","ITEMSKUCODE":"BT102","SALESRATE":100.0,"PURCHASERATE":5.0,"DOE":"~","STOCKQTY":2.0,"PHYSICALQTY":1.0}] ;

you can access it via AlreadySaved[0]["PHYSICALQTY"]

Anurag Sinha
  • 1,014
  • 10
  • 17
0

firstly you have to serialize your object like this;

var json_data = JSON.parse(AlreadySaved);

and if you know specific data in json_data, you can call with index number like this;

var result= json_data[0]["PHYSICALQTY"];

Or you can find whatever you want, you can find with foreach;

for(var item in json_data) {
   alert(item["PHYSICALQTY"].value);
}
kemal akoğlu
  • 182
  • 13
0

Please try this:

//In case you are getting this from ajax request put "returnObject.Json_Variable" instead of "Json_Variable"
var obj = (JSON.parse("Json_Variable"));
var resultPHYSICALQTY=obj.PHYSICALQTY);
Nayan Katkani
  • 806
  • 8
  • 18
  • @Shreya, In case you have any query or problem please feel free to ping me and if you find it helpful mark it as answer so it will help others too – Nayan Katkani Dec 23 '16 at 07:49