2

I am trying to load a Local JSON file by jquery. the code works fine but the data are not available in array.

$.getJSON("/ajax/data/myjasonfile.json", function(json) {
        console.log(json);
    });

my console shows only

Object {data: Array[1]}

my sample json

{
    "data": [
        {
            "number": "1234",
            "nameOne": "Laten Thamn",
            "type": "Fishing Vessels",
            "flagName": "5467",
            "buildDate":"12/08/2016"
        }
    ]
}
7
  • 1
    "the data are not available in array" what do you mean ? Commented Dec 22, 2016 at 11:52
  • Do you mean local to the client machine? If so, this is not possible. If you mean local to the server, then what you have should work fine, assuming your path is valid. Commented Dec 22, 2016 at 11:54
  • The data should be available as an object. What does your console say Commented Dec 22, 2016 at 11:54
  • but i have above 100 records in the json Commented Dec 22, 2016 at 11:56
  • you should to check database query. Commented Dec 22, 2016 at 12:08

4 Answers 4

3

Try this to understand your response structure.

 $.getJSON("myjasonfile.json", function(json) {
        console.log(json); // access the response object
        console.log(json.data); // access the array
        console.log(json.data[0]); // access the first object of the array
        console.log(json.data[0].number); // access the first object proprty of the array
    });
Sign up to request clarification or add additional context in comments.

1 Comment

glad to help :)
0

jQuery.getJSON() load JSON-encoded data from the server using a GET HTTP request.

As you post in your JSON structure you can access to the array by calling json.data:

$.getJSON("/ajax/data/myjasonfile.json", function(json) {
     console.log(json.data); // Logs your array
});

1 Comment

If it's showing Object {data: Array1} it's already an object
0

Store The json Data in Array:

 $.getJSON("/ajax/data/myjasonfile.json", function(Data) {
        var array=[];
    for(var i=1;i<Data.length;i++)
    {
        var b=Data[i];
        var c=[];
        c.push(b.number);
        c.push(b.nameOne);
        c.push(b.type);
        c.push(b.flagName);
        c.push(b.buildDate);
        array.push(c);
    }
        console.log(array);
    });

2 Comments

May I suggest you add some context explaining your answer?
Thank you for this code snippet, which may provide some immediate help. A proper explanation would greatly improve its educational value by showing why this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.
-1

you can try this:

$.getJSON("test.json", function(json) {
    console.log(json); // this will show the info it in firebug console
});

I think, Some issue is in your path to call json file. you need to check again of path.

You can check this link: Loading local JSON file

2 Comments

You should to follow above link. it will help you.
so your saying this is a duplicate?

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.