www.webdeveloper.com
Results 1 to 6 of 6

Thread: parse the log and output in json format

  1. #1
    Join Date
    Mar 2014
    Posts
    8

    parse the log and output in json format

    Learnt a lot from previous posts, but I am still stuck by how to have a json format output. So I am trying to parse the log and have a json format output.

    The log looks like:

    2014-02-25T01:00:44+0000,v=5,sv=1,ss=active,es=-,ai=e6e32a23-369b-4da3-b2da-630aee75f8c5,ip='99.114.39',rm=GET,rv=HTTP/1.1,rs=200,rt=0.787020,ru='/tag/febe1?l=http%3A%2F%2Fwww.michaels.com%2Fvideosoking',rr='http://www.michaels.com/?fdid=videos-scrapbooking',ua='Mozilla/5.0'

    The log after parse is like:

    2014-02-25T01:00:44+0000,
    v=5,
    sv=1,
    ss=active,
    es=-,
    ai=e6e32a23-369b-4da3-b2da-630aee75f8c5,
    ip='99.114...


    But I want this output in json format, I am quite new in js, and not sure where should I start/fix my code, my code is following:

    <!doctype html>
    <html>
    <head>
    <title>reading file</title>
    <script>
    function readText(obj){
    var file = obj.files[0],
    div=document.getElementById('main');
    if(file){
    div.innerHTML='';
    var reader = new FileReader();
    reader.onerror = function(event){
    div.innerHTML='The file can\'t be read! Error ' + event.target.error.code;
    }
    reader.onload = function(event){
    var log = event.target.result.split("\n");
    for(var i=0; i<log.length; i++){
    var str = log[i].split(',');
    div.innerHTML+='EventTime='+str[0]+'<br />';
    for (var j=0; j<str.length; j++) {
    var name = str[j].split('=');
    if (name[0]=="ci"||name[0]=='si'||name[0]=='ip') {
    div.innerHTML+=str[j]+'<br />';
    }else {
    continue;
    }
    }
    div.innerHTML+='<br />';

    }
    }
    reader.readAsText(file);
    }
    }

    window.onload=function(){
    document.getElementById('ff').onchange=function(){readText(this);}
    }

    </script>
    </head>
    <body>
    <input type="file" id="ff" />
    <div id="main"></div>
    </body>

    </html>

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    868
    There are a couple of ways you could handle this. Honestly I feel like changing how the log is actually saved would be the best way, but of course that depends on if you are even able to change how the log gets saved. If you can, you would want to structure the data like a JSON object which could be easily parsed in javascript and instantly converted to a useable JSON object. If not, you'll have to convert it yourself.

    My code here could be tweaked a little bit, but hopefully it will help you understand exactly how this works.
    It's designed to read through your log file (like you already had) and add each property (eg. ip, sv, ai, rv) and its value to a JSON structured object.
    Code:
    <!doctype html>
    <html>
    <head>
    	<title>reading file</title>
    	<script>
    		var $jsonData = new Array();
    		function readText(obj) {
    			var file = obj.files[0];
    			if(file) {
    				var reader = new FileReader();
    				reader.onerror = function(event) {
    					div.innerHTML = 'The file can\'t be read! Error ' + event.target.error.code;
    				}
    				reader.onload = function(event) {
    					var log = event.target.result.split("\n");
    					for(var i = 0; i < log.length; i++) {
    						var str = log[i].split(',');
    						if(str.length > 0) {
    							$jsonData[i] = new Array();
    							$jsonData[i]['event_time'] = str[0];
    							for(var j = 1; j < str.length; j++) {
    								var name = str[j].split('=');
    								$jsonData[i][name[0]] = name[1];
    							}
    						}
    					}
    				}
    				reader.readAsText(file);
    			}
    		}
    
    		window.onload = function() {
    			document.getElementById('ff').onchange = function(){ readText(this); }
    		}
    	</script>
    </head>
    <body>
    	<input type="file" id="ff" />
    	<div id="main"></div>
    </body>
    </html>
    This does not print anything to the page. If you wanted to do that you can easily loop through the $jsonData array or you can pull out specific values that you need.
    Code:
    // List everything via loop
    		function _ListData() {
    			$div = document.getElementById('main');
    			$div.innerHTML = '';
    			for(var $i = 0; $i < $jsonData.length; $i++) {
    				for(var $name in $jsonData[$i]) {
    					$div.innerHTML += $name + ' = ' + $jsonData[$i][$name] + '<br />';
    				}
    				$div.innerHTML += '<br />';
    			}
    		}
    Pulling out specific values would simply use some sort of call like '$jsonData[0][ip]' which would return the value of ip for the first log entry/line.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Mar 2014
    Posts
    8
    Thanks a lot. It really helps! It works!

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    suspect the next time it's going to be about the log genetic examination or radiocarbon dating ))
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  5. #5
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    868
    Quote Originally Posted by Padonak View Post
    suspect the next time it's going to be about the log genetic examination or radiocarbon dating ))
    Who knows, we could be helping build a cyber-weapon of mass destruction and we don't even know it.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648

    Thumbs up

    :d
    Last edited by Padonak; 03-07-2014 at 12:06 AM.
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles