Search code examples
javascriptjsonfor-loopdata-analysis

JavaScript JSON Combination and Data Anylish


I am trying to add values from multiple JSON responses that are saved in a .txt file. The .txt files has about 4000 entries. They are each the same format as follows:

{"id":"8f546dcf-b66a-4c53-b3d7-7290429483b8","price":"247.96000000","size":"0.03121005","product_id":"BTC-USD","side":"sell","stp":"dc"}

{"id":"0ec4b63a-b736-42af-a0aa-b4581bf12955","price":"247.90000000","size":"0.03910014","product_id":"BTC-USD","side":"sell","stp":"dc"}

{"id":"be403848-74dc-4494-8095-bd468777c958","price":"247.89000000","size":"0.04280854","product_id":"BTC-USD","side":"sell","stp":"dc"}

{"id":"ae2ae129-e850-4d8f-b945-55e65eb68a88","price":"247.83000000","size":"0.07941840","product_id":"BTC-USD","side":"sell","stp":"dc"}

{"id":"96194be4-40d8-446d-9f7e-ce72bc84af48","price":"247.63000000","size":"0.06225897","product_id":"BTC-USD","side":"sell","stp":"dc"}

I believe I need to combine the different JSON data sets before I can loop through them with a for loop and do the summing/analysis part (size:JSONSET1 + size:JSONSET2 + .....) but I'm not sure how I should call the .txt file in javascript and have it combine the multiple json parts. Suggestions??


Solution

  • I would recommend using a .json file since you are working with JSON{Objects}. I've made a demo of the file here and I've made a demo of the data analysis here.

    Note: Demo file is hosted in a personal server, so it may not work later on.

    Now the file isn't proper JSON Syntax, so it needs some parsing.

    entries.json

    {"id":"8f546dcf-b66a-4c53-b3d7-7290429483b8","price":"247.96000000","size":"0.03121005","product_id":"BTC-USD","side":"sell","stp":"dc"}
    
    {"id":"0ec4b63a-b736-42af-a0aa-b4581bf12955","price":"247.90000000","size":"0.03910014","product_id":"BTC-USD","side":"sell","stp":"dc"}
    
    (..)
    

    JavaScript

    Req = new XMLHttpRequest();
    Req.onload = process_entries;
    Req.open("get", "http://butler.tk/entries.json", true);
    Req.send();
    
    function process_entries() {
        var response = Req.responseText;
        var arrayed = "[" + response
                            .split("}")
                            .join("},")
                            .slice(0,-1)
                     + "]";
        var entries = JSON.parse(arrayed);
        for (var i = 0, l = entries.length; i < l; i++) {
            var entry = entries[i];
            //entry.id,entry.size,etc.
        }
    }
    
    • We fetch the file with a XMLHttpRequest().
    • We parse the file so that it's valid JSON Syntax, the file will now look like this.

    entries.json (parsed)

    [
        {"id":"8f546dcf-b66a-4c53-b3d7-7290429483b8","price":"247.96000000","size":"0.03121005","product_id":"BTC-USD","side":"sell","stp":"dc"},
    
        {"id":"0ec4b63a-b736-42af-a0aa-b4581bf12955","price":"247.90000000","size":"0.03910014","product_id":"BTC-USD","side":"sell","stp":"dc"},
    
        (..)
    ]
    
    • We parse the file into a JSON{Object}
    • We iterate through the array of objects and access their properties.

    Note: If you have control over how the data is saved, you can save the data in the array format instead of parsing it.

    Hope it helps! :)