javascripthtmljsonhar

How to access the data of .har file with JSON format?


I have a .har file which I generated from a website. I think that a .har file is a JSON object (from the search i did on the web) and i want to access some specific data, like the serverIPAddress, startedDateTime and some mores. The question is how i can access them.

This is the HTML code where the user will upload his .har file:

<!DOCTYPE html>
<html>
    <head>
        <title>SelectFile</title>
    </head>
    <body>
        <h1>Please select your HAR files</h1>
        <input type="file" id="logo" accept=".HAR" multiple>
    </body>
</html>

<style type="text/css">
    h1{text-align:center;}
    body {font-family: Arial, Helvetica, sans-serif;}
    form {border: 3px solid #f1f1f1;}

    input[type=text], input[type=password] {
      width: 100%;
      padding: 12px 20px;
      margin: 8px 0;
      display: inline-block;
      border: 1px solid #ccc;
      box-sizing: border-box;
    }
</style>

This is the script of javascript:

<script type="text/javascript"> 
    const fileselector = document.getElementById('logo')
    console.log(fileselector);

    fileselector.addEventListener('change', function() { 

        const file= this.files[0];
        console.log(file);
        console.log(file.entries);//This prints error message in console
</script>

I am kind of new in javascript and i know generally that you can access an object like this objectName.propertyName. Thanks in advance.


Solution

  • The File object first has to be read, and the resulting JSON parsed, before you can do anything. Try something like this:

    const dataString = await this.files[0].text();
    const data = JSON.parse(dataString);
    

    See also: