How to merge content of two or more JSON files with Framework7?
I have two json objects and I have been looking for a way to combine these two objects into one object.
app.request.json('test_1.json')
.then(function (res) {
console.log(res.data);
});
app.request.json('test_2.json')
.then(function (res) {
console.log(res.data);
});
deejay
2
Promise.all([
app.request.json('test_1.json').then(r => r.data),
app.request.json('test_2.json').then(r => r.data)
]).then(a => console.log(a)) // [test_1,test_2]
2 Likes
Tim
3
To merge the JSON objects into a single object, you can use app.utils.extend
Promise.all([
app.request.json('test_1.json').then(r => r.data),
app.request.json('test_2.json').then(r => r.data)
]).then(data_parts => app.utils.extend({}, data_parts[0], data_parts[1]) )
2 Likes
Thank you very much!
How can I access the content, e.g. via console log?
deejay
5
2 Likes
Thank you very much for that!
But I still have one last question:
How do I use the function with the following line of code?
app.request.json('xxxxx.json', function(data) {
....
....
deejay
7
i don’t understand your question
post your (real) code and i’ll try to figure it out
Currently only one JSON file is read in and processed. But I want to read in 2 files (same structure, only different data).
app.request.json('quiz_1.json', function(data) {
for (i = 0; i < data.quizlist.length; i++) {
questionBank[i] = new Array;
questionBank[i][0] = data.quizlist[i].question;
questionBank[i][1] = data.quizlist[i].option1;
questionBank[i][2] = data.quizlist[i].option2;
questionBank[i][3] = data.quizlist[i].option3;
}
numberOfQuestions = questionBank.length;
})
The JSON files looks like:
{
"quizlist": [{
"question": "What is ....?",
"option1": "a",
"option2": "b",
"option3": "c"
},
{
"question": "What is....?",
"option1": "1",
"option2": "2",
"option3": "3"
},
....
deejay
9
if your json files are static then you better load them at complie/build time
// all data
import data from '/path/to/file.json';
// or using key:
import { quizlist } from '/path/to/quiz_1.json';
in case your json is dynamic => async-morning-cpxt4r - CodeSandbox