fetch from two json files data ajax node





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















The JSON files src1 and src2 contain a list of video items. I created a web page that will initially load data via AJAX from src1.json and src2.json. The only difference is that src2.json has rating field; how can i combine them to make only one call in ajax to present in an htmlfile? Is it possible to be done with node.js and ajax?



src1.json



[
{
'id' : 67544,
'title' : 'Title 7',
'posttime' : '2017-03-21T10:56:11.404Z',
'tags' : [
'green',
'red',
'blue'
]
},
{
'id' : 333,
'title' : 'Title 9',
'posttime' : '2017-01-01T09:01:10.404Z',
'tags' : [
'orange',
'purple'
]
},
{
'id' : 444,
'title' : 'Title 8',
'posttime' : '2017-03-02T07:12:00.404Z',
'tags' : [
'magenta',
'red',
'yellow'
]
}
]


src2.json



[
{
'id' : 43423,
'title' : ' Title 1',
'posttime' : '2017-03-21T10:56:42.404Z',
'rating' : 1000,
'tags' : [
'green',
'red',
'blue'
]
},
{
'id' : 342334,
'title' : 'Title 2',
'posttime' : '2017-03-21T09:56:42.404Z',
'rating' : 100,
'tags' : [
'orange',
'purple'
]
},
{
'id' : 66343,
'title' : 'Title 3',
'posttime' : '2017-03-21T10:52:42.404Z',
'rating' : 30,
'tags' : [
'magenta',
'red',
'yellow'
]
}
]


I tried:



$(document).ready(function() {
var url = "src1.json";
var url2 = "src1.json";

enter code here

$("button").click(function() {
$.get(url, function(data) {
$('#mydiv').append((JSON.stringify(data)));
});
$.get(url2, function(data) {
$('#mydiv').append((JSON.stringify(data)));
});

});
});









share|improve this question

























  • Hi and welcome to the community. What have you tried so far? The purpose of SO is not for other people to write the program for you, but we will happily help you if can provide more specific problems (for example: "I tried this approach but it doesn't work since only half the items are included" is a good problem description.) Please see this web page for more information: stackoverflow.com/help/how-to-ask

    – Per Lundberg
    Nov 16 '18 at 21:27











  • updated check it

    – Constantinos Sergiou
    Nov 16 '18 at 21:48


















0















The JSON files src1 and src2 contain a list of video items. I created a web page that will initially load data via AJAX from src1.json and src2.json. The only difference is that src2.json has rating field; how can i combine them to make only one call in ajax to present in an htmlfile? Is it possible to be done with node.js and ajax?



src1.json



[
{
'id' : 67544,
'title' : 'Title 7',
'posttime' : '2017-03-21T10:56:11.404Z',
'tags' : [
'green',
'red',
'blue'
]
},
{
'id' : 333,
'title' : 'Title 9',
'posttime' : '2017-01-01T09:01:10.404Z',
'tags' : [
'orange',
'purple'
]
},
{
'id' : 444,
'title' : 'Title 8',
'posttime' : '2017-03-02T07:12:00.404Z',
'tags' : [
'magenta',
'red',
'yellow'
]
}
]


src2.json



[
{
'id' : 43423,
'title' : ' Title 1',
'posttime' : '2017-03-21T10:56:42.404Z',
'rating' : 1000,
'tags' : [
'green',
'red',
'blue'
]
},
{
'id' : 342334,
'title' : 'Title 2',
'posttime' : '2017-03-21T09:56:42.404Z',
'rating' : 100,
'tags' : [
'orange',
'purple'
]
},
{
'id' : 66343,
'title' : 'Title 3',
'posttime' : '2017-03-21T10:52:42.404Z',
'rating' : 30,
'tags' : [
'magenta',
'red',
'yellow'
]
}
]


I tried:



$(document).ready(function() {
var url = "src1.json";
var url2 = "src1.json";

enter code here

$("button").click(function() {
$.get(url, function(data) {
$('#mydiv').append((JSON.stringify(data)));
});
$.get(url2, function(data) {
$('#mydiv').append((JSON.stringify(data)));
});

});
});









share|improve this question

























  • Hi and welcome to the community. What have you tried so far? The purpose of SO is not for other people to write the program for you, but we will happily help you if can provide more specific problems (for example: "I tried this approach but it doesn't work since only half the items are included" is a good problem description.) Please see this web page for more information: stackoverflow.com/help/how-to-ask

    – Per Lundberg
    Nov 16 '18 at 21:27











  • updated check it

    – Constantinos Sergiou
    Nov 16 '18 at 21:48














0












0








0








The JSON files src1 and src2 contain a list of video items. I created a web page that will initially load data via AJAX from src1.json and src2.json. The only difference is that src2.json has rating field; how can i combine them to make only one call in ajax to present in an htmlfile? Is it possible to be done with node.js and ajax?



src1.json



[
{
'id' : 67544,
'title' : 'Title 7',
'posttime' : '2017-03-21T10:56:11.404Z',
'tags' : [
'green',
'red',
'blue'
]
},
{
'id' : 333,
'title' : 'Title 9',
'posttime' : '2017-01-01T09:01:10.404Z',
'tags' : [
'orange',
'purple'
]
},
{
'id' : 444,
'title' : 'Title 8',
'posttime' : '2017-03-02T07:12:00.404Z',
'tags' : [
'magenta',
'red',
'yellow'
]
}
]


src2.json



[
{
'id' : 43423,
'title' : ' Title 1',
'posttime' : '2017-03-21T10:56:42.404Z',
'rating' : 1000,
'tags' : [
'green',
'red',
'blue'
]
},
{
'id' : 342334,
'title' : 'Title 2',
'posttime' : '2017-03-21T09:56:42.404Z',
'rating' : 100,
'tags' : [
'orange',
'purple'
]
},
{
'id' : 66343,
'title' : 'Title 3',
'posttime' : '2017-03-21T10:52:42.404Z',
'rating' : 30,
'tags' : [
'magenta',
'red',
'yellow'
]
}
]


I tried:



$(document).ready(function() {
var url = "src1.json";
var url2 = "src1.json";

enter code here

$("button").click(function() {
$.get(url, function(data) {
$('#mydiv').append((JSON.stringify(data)));
});
$.get(url2, function(data) {
$('#mydiv').append((JSON.stringify(data)));
});

});
});









share|improve this question
















The JSON files src1 and src2 contain a list of video items. I created a web page that will initially load data via AJAX from src1.json and src2.json. The only difference is that src2.json has rating field; how can i combine them to make only one call in ajax to present in an htmlfile? Is it possible to be done with node.js and ajax?



src1.json



[
{
'id' : 67544,
'title' : 'Title 7',
'posttime' : '2017-03-21T10:56:11.404Z',
'tags' : [
'green',
'red',
'blue'
]
},
{
'id' : 333,
'title' : 'Title 9',
'posttime' : '2017-01-01T09:01:10.404Z',
'tags' : [
'orange',
'purple'
]
},
{
'id' : 444,
'title' : 'Title 8',
'posttime' : '2017-03-02T07:12:00.404Z',
'tags' : [
'magenta',
'red',
'yellow'
]
}
]


src2.json



[
{
'id' : 43423,
'title' : ' Title 1',
'posttime' : '2017-03-21T10:56:42.404Z',
'rating' : 1000,
'tags' : [
'green',
'red',
'blue'
]
},
{
'id' : 342334,
'title' : 'Title 2',
'posttime' : '2017-03-21T09:56:42.404Z',
'rating' : 100,
'tags' : [
'orange',
'purple'
]
},
{
'id' : 66343,
'title' : 'Title 3',
'posttime' : '2017-03-21T10:52:42.404Z',
'rating' : 30,
'tags' : [
'magenta',
'red',
'yellow'
]
}
]


I tried:



$(document).ready(function() {
var url = "src1.json";
var url2 = "src1.json";

enter code here

$("button").click(function() {
$.get(url, function(data) {
$('#mydiv').append((JSON.stringify(data)));
});
$.get(url2, function(data) {
$('#mydiv').append((JSON.stringify(data)));
});

});
});






node.js ajax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 22:30









Brian Mains

44.2k32129233




44.2k32129233










asked Nov 16 '18 at 19:36









Constantinos SergiouConstantinos Sergiou

3114




3114













  • Hi and welcome to the community. What have you tried so far? The purpose of SO is not for other people to write the program for you, but we will happily help you if can provide more specific problems (for example: "I tried this approach but it doesn't work since only half the items are included" is a good problem description.) Please see this web page for more information: stackoverflow.com/help/how-to-ask

    – Per Lundberg
    Nov 16 '18 at 21:27











  • updated check it

    – Constantinos Sergiou
    Nov 16 '18 at 21:48



















  • Hi and welcome to the community. What have you tried so far? The purpose of SO is not for other people to write the program for you, but we will happily help you if can provide more specific problems (for example: "I tried this approach but it doesn't work since only half the items are included" is a good problem description.) Please see this web page for more information: stackoverflow.com/help/how-to-ask

    – Per Lundberg
    Nov 16 '18 at 21:27











  • updated check it

    – Constantinos Sergiou
    Nov 16 '18 at 21:48

















Hi and welcome to the community. What have you tried so far? The purpose of SO is not for other people to write the program for you, but we will happily help you if can provide more specific problems (for example: "I tried this approach but it doesn't work since only half the items are included" is a good problem description.) Please see this web page for more information: stackoverflow.com/help/how-to-ask

– Per Lundberg
Nov 16 '18 at 21:27





Hi and welcome to the community. What have you tried so far? The purpose of SO is not for other people to write the program for you, but we will happily help you if can provide more specific problems (for example: "I tried this approach but it doesn't work since only half the items are included" is a good problem description.) Please see this web page for more information: stackoverflow.com/help/how-to-ask

– Per Lundberg
Nov 16 '18 at 21:27













updated check it

– Constantinos Sergiou
Nov 16 '18 at 21:48





updated check it

– Constantinos Sergiou
Nov 16 '18 at 21:48












1 Answer
1






active

oldest

votes


















0














I think that what you are aiming for is something like:




  1. Load src1.json, and parse it.

  2. Load src2.json, and parse it.

  3. Combine the arrays produced to a new array.


So if the question is: can you make an HTTP backend in node.js that does this? The answer to that question is clearly: yes, it can be done. Take a look at e.g. Expressjs: https://expressjs.com/, it's a framework for easily creating HTTP web applications with node.js.



Actually concatenating the arrays in Javascript should be simple, once you have read the files into memory and parsed them. Something like this should do:



$ node
> a = [1,2,3]
[ 1, 2, 3 ]
> b = [4,5,6]
[ 4, 5, 6 ]
> a.concat(b)
[ 1, 2, 3, 4, 5, 6 ]





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53344289%2ffetch-from-two-json-files-data-ajax-node%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I think that what you are aiming for is something like:




    1. Load src1.json, and parse it.

    2. Load src2.json, and parse it.

    3. Combine the arrays produced to a new array.


    So if the question is: can you make an HTTP backend in node.js that does this? The answer to that question is clearly: yes, it can be done. Take a look at e.g. Expressjs: https://expressjs.com/, it's a framework for easily creating HTTP web applications with node.js.



    Actually concatenating the arrays in Javascript should be simple, once you have read the files into memory and parsed them. Something like this should do:



    $ node
    > a = [1,2,3]
    [ 1, 2, 3 ]
    > b = [4,5,6]
    [ 4, 5, 6 ]
    > a.concat(b)
    [ 1, 2, 3, 4, 5, 6 ]





    share|improve this answer




























      0














      I think that what you are aiming for is something like:




      1. Load src1.json, and parse it.

      2. Load src2.json, and parse it.

      3. Combine the arrays produced to a new array.


      So if the question is: can you make an HTTP backend in node.js that does this? The answer to that question is clearly: yes, it can be done. Take a look at e.g. Expressjs: https://expressjs.com/, it's a framework for easily creating HTTP web applications with node.js.



      Actually concatenating the arrays in Javascript should be simple, once you have read the files into memory and parsed them. Something like this should do:



      $ node
      > a = [1,2,3]
      [ 1, 2, 3 ]
      > b = [4,5,6]
      [ 4, 5, 6 ]
      > a.concat(b)
      [ 1, 2, 3, 4, 5, 6 ]





      share|improve this answer


























        0












        0








        0







        I think that what you are aiming for is something like:




        1. Load src1.json, and parse it.

        2. Load src2.json, and parse it.

        3. Combine the arrays produced to a new array.


        So if the question is: can you make an HTTP backend in node.js that does this? The answer to that question is clearly: yes, it can be done. Take a look at e.g. Expressjs: https://expressjs.com/, it's a framework for easily creating HTTP web applications with node.js.



        Actually concatenating the arrays in Javascript should be simple, once you have read the files into memory and parsed them. Something like this should do:



        $ node
        > a = [1,2,3]
        [ 1, 2, 3 ]
        > b = [4,5,6]
        [ 4, 5, 6 ]
        > a.concat(b)
        [ 1, 2, 3, 4, 5, 6 ]





        share|improve this answer













        I think that what you are aiming for is something like:




        1. Load src1.json, and parse it.

        2. Load src2.json, and parse it.

        3. Combine the arrays produced to a new array.


        So if the question is: can you make an HTTP backend in node.js that does this? The answer to that question is clearly: yes, it can be done. Take a look at e.g. Expressjs: https://expressjs.com/, it's a framework for easily creating HTTP web applications with node.js.



        Actually concatenating the arrays in Javascript should be simple, once you have read the files into memory and parsed them. Something like this should do:



        $ node
        > a = [1,2,3]
        [ 1, 2, 3 ]
        > b = [4,5,6]
        [ 4, 5, 6 ]
        > a.concat(b)
        [ 1, 2, 3, 4, 5, 6 ]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 21:58









        Per LundbergPer Lundberg

        1,81312433




        1,81312433
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53344289%2ffetch-from-two-json-files-data-ajax-node%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python