Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Sam Donald
36,305 PointsLimiting xhr.responseText to only specific object in JSON file
Preamble:
- I'm implementing a
new XMLHttpRequest()via a Promise. And using theGETmethod. - The project uses plain javascript with no server-side scripting.
- I have a
JSONfile that's formatted as follows. And the path to the that file is'json/users.json'.
[
{
"name" : "John Smith",
"avatar" : "john_smith.webp",
"bio" : "Some words from John Smith",
"projects" : [
{
"number" : 5,
"date" : "09 March 2017",
"time" : "08:34PM",
"link" : "link-to-project-attempt",
},
{
"number" : 12,
"date" : "09 March 2017",
"time" : "07:14PM",
"link" : "link-to-project-attempt",
}
]
},
{
"name" : "Gill Jenkins",
"avatar" : "gill_jenkins.webp",
"bio" : "Some words from Gill Jenkins",
"projects" : [
{
"number" : 9,
"date" : "12 Feb 2017",
"time" : "20:04PM",
"link" : "link-to-project-attempt",
},
{
"number" : 24,
"date" : "23 Jan 2017",
"time" : "17:45PM",
"link" : "link-to-project-attempt",
}
]
}
]
The Problem
I am successfully making the call to the file, but the responseText contains the entire JSON file.
This file may potentially hold hundreds or thousands of unique users. Each of which could have 100+ projects.
Since I'm only ever going to need one user at a time I'd like to be able to restrict the responseText to only the object containing an instance of that user.
I had thought I could do a simple query string like this:
xhr.open('GET', 'json/users.json?name=Gill%20Jenkins');
But that still returns the entire JSON file.
So is this doable with a solely front-end workflow or am I out of luck?
I have a gut feeling that it should be achievable.
Thanks for the assist.
Answers
Please don't supply answers/solutions that require jQuery or other dependencies. This is a plain (vanilla) javascript project.
1 Answer
Chris Shaw
26,676 PointsHi Sam Donald,
I'd like to say it's possible, but it sadly isn't. You would need a backend handler to process the JSON and parse out the data you want. That said, storing large data sets in JSON won't benefit you in the future. As the file becomes larger, it will take longer to parse it causing lengthy delays when requesting data.
You may want to look into 'No Database' solutions such as lowdb which is a JSON handler for NodeJS which provides persistence and memory caching which are two critical things as you don't want to be requesting the same data more than once.
Back to your original question, no, it's not possible with just JavaScript in the browser.
Sam Donald
36,305 PointsSam Donald
36,305 PointsThanks Chris Upjohn,
I sort of re-evaluated myself a little after posting this one. And I figured you could't do it because you can't implement any methods etc... in the
JSONfile, so there's no why it could sort through itself. I guess you could describeJSONfiles as dumb.I'm looking into a solution with Google Spreadsheets now. And I think I'll take a look at that Build a Realtime Database app with Firebase workshop Treehouse just released.