4

I have the following ajax call and the json feed it returns. how do I get the value of the data object i.e. FRI from the feed using jquery?

$.ajax({
    url: query,
    type: "GET",
    dataType: "json"
    success: function(data) {
        var day = // get data value from json
        $("#Day").val(day);
    }
});    

{
   "name":"workdays",
   "columns":[
      "day"
   ],
   "data":[
      [
         "FRI"
      ]
   ]
}     

* update *

What would be the syntax be if the results were returned as jsonp as follows, how can you extract the value 'FRI' :

import({
  "Results":{
    "work_days":{
        "empid":100010918994,
        "day":"FRI"
     }
  }
});
12
  • 1
    You access it just as you would a normal Javascript object. I'm not too sure I understand what exactly you are asking... data.data? Commented Dec 7, 2015 at 14:59
  • day = data['data'] ? Commented Dec 7, 2015 at 14:59
  • it's json. (j)ava(s)cript (o)bject (n)otation. you access the decoded json like you would any other javascript data structure, because json IS javascript. Commented Dec 7, 2015 at 15:02
  • @MarcB: Saying "json IS javascript" is very misleading. Its syntax looks like a subset of JavaScript, but then some of JavaScript looks like C. So would we say JavaScript IS C? In fact, JSON/JavaScript are even more distinct since JSON is not in any way a programming language. Commented Dec 7, 2015 at 15:07
  • it may be a subset, but far too many people have the idea that json is magically special and requires special handling. Commented Dec 7, 2015 at 15:10

7 Answers 7

4

This is just JavaScript, not jQuery.

var data = {
   "name":"workdays",
   "columns":[
      "day"
   ],
   "data":[
      [
         "FRI"
      ]
   ]
}

data.data[0][0]; //FRI

UPDATE

var obj = {
  "Results":{
    "work_days":{
        "empid":100010918994,
        "day":"FRI"
     }
  }
}

obj.Results.work_days.day //FRI
Sign up to request clarification or add additional context in comments.

1 Comment

How can you get the same value if the results were returned as jsonp - see updated post.
3

If the latter json is the json you get from the server, you can get the data like this:

var day = data.data[0][0];

This will put the value FRI in the variable day.

EDIT: If you use a recent browser, you can always do console.log(data) and look in your javascript console what is in the variable

Comments

1
    $.ajax({
    url: query,
    type: "GET",
    dataType: "json"
    success: function(data) {
        var day = data.data[0][0] // get data value from json
        $("#Day").val(day);
    }
});    

{
   "name":"workdays",
   "columns":[
      "day"
   ],
   "data":[
      [
         "FRI"
      ]
   ]
} 

1 Comment

OP is asking how to get 'FRI', not 'day'.
0

json is javascript object, so you can access to any his property:

data.name
data.columns
data.data

Comments

0

You don't need jquery data is a javascript object (hash). You can access the data as follow:

data.columns

Or

data["columns"]

Comments

0

For retrieving the 'data' field

$.ajax({
url: query,
type: "GET",
dataType: "json"
success: function(data) {
    var day = data['data']; // get data value from json
    $("#Day").val(day);
}
});    

{
   "name":"workdays",
   "columns":[
   "day"
],
"data":[
   [
     "FRI"
   ]
]
} 

Comments

0

Have you ever checked the data.d variable?

success: function(data) {
   console.log(data.d); //prints the value
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.