oshirowanen
oshirowanen

Reputation: 15925

Extract data from json file

If I have a static json file on my server, is it possible to extract only the required section of the json data using javascript without downloading the whole json file?

I understand that I can use serverside technologies to dynamically generate json data to based on what I want, but I just wanted to know if javascript can be used to extract data from a server json file without downloading the whole thing?

Upvotes: 0

Views: 744

Answers (3)

w3jimmy
w3jimmy

Reputation: 712

the answer is no, but if you could handle the json data on server side, you could create a json response when sending parameters on your ajax call to retrieve different results depending on the parameters you sent. These parameters would limit which sections you want.

Upvotes: 0

Roy Dictus
Roy Dictus

Reputation: 33139

For a static file on the server, how would JavaScript access it without downloading the whole file?

I'm afraid this is impossible. You could download the file and filter through the data on the client, or you could write server-side code to do the filtering for you, then output the result to the caller -- your client-side JavaScript code.

Upvotes: 1

Denys Séguret
Denys Séguret

Reputation: 382132

No, you can't do this client side as the browser called by javascript can only fetch a whole file.

If you really need to get only part of the file, you'll have to build server side code.

But check before by profiling that you need to do this optimization. And don't forget that fetching only part of a file would prevent the file from being cached and reused as a whole.

Upvotes: 3

Related Questions