Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm building an image search app using Pexels, Flickr, Pixabay and Unsplash API's and I want to display the results of all searches. However each search result from each API returns different JSON files with different keys. How do I combine/format them in to a single JSON file in order to display all the search results on my page?

What I have tried:

Unsplash.js code eg:

function search(searchTerm) {
const url = `${API_URL}&query=${searchTerm}`;
loadingImage.style.display = '';
imageSection.innerHTML = '';
return fetch(url)
.then(response => response.json())
.then(result => {
return result.results;
});
}

function displayImages(images) {
images.forEach(image => {
const imageElement = document.createElement('img');
imageElement.src = image.urls.regular;
imageSection.appendChild(imageElement);
});
loadingImage.style.display = 'none';
}


Flickr.js eg:

function search(searchTerm) {
const url = `${API_URL}&tags=${searchTerm}&per_page=500&license=7,8,9&format=json&nojsoncallback=1`;
loadingImage.style.display = '';
imageSection.innerHTML = '';
return fetch(url)
.then(response => response.json())
.then(result => {
return (result.photos.photo);
});
}

function displayImages(images) {
images.forEach(photo => {
const imageElement = document.createElement('img');
imageElement.src = "http://farm" + photo.farm + ".static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + "_" + "z.jpg";
imageSection.appendChild(imageElement);
});
loadingImage.style.display = 'none';
}
Posted
Updated 27-Sep-18 8:59am
Comments
littleGreenDude 27-Sep-18 16:07pm    
I would think you would need to read each in to its own respective JS object and then convert from each object to your own common object (or one of the existing other objects). Thinking of it in terms of combining JSON is not really the right approach. The JSON you are receiving is the serialization of distinct objects from disparate sources. You need to create the common ground.
[no name] 27-Sep-18 17:37pm    
Since you are the "consumer", HOW it comes together is YOUR problem.

You got apples, oranges, pears, etc. and you want a fruit salad.

How else do make a fruit salad?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900