Jump to content
UBot Underground

I Need To Build A Json File


Recommended Posts


// path to file
$file="1_23.csv";

// reads in the file
$csv= file_get_contents($file);

// creates array of data
$array = array_map("str_getcsv", explode("\n", $csv));

// encodes to json
$json = json_encode($array);

// returns json data
print_r($json);
Link to post
Share on other sites
set table cell(&myT,0,0,"Apples")
set table cell(&myT,0,1,"Oranges")
set table cell(&myT,1,0,1)
set table cell(&myT,1,1,3)
run javascript("//var csv is the CSV file with headers
function csvJSON(csv)\{

  var lines=csv.split(\"\\n\");

  var result = [];

  var headers=lines[0].split(\",\");

  for(var i=1;i<lines.length;i++)\{

	  var obj = \{\};
	  var currentline=lines[i].split(\",\");

	  for(var j=0;j<headers.length;j++)\{
		  obj[headers[j]] = currentline[j];
	  \}

	  result.push(obj);

  \}
  
  //return result; //JavaScript object
  return JSON.stringify(result); //JSON
\}")
alert($eval("csvJSON(`{&myT}`)"))

Remember the little ` ` marks for the eval bit, if your csv's have that symbol the code will break but I doubt they would

**Does not work with Chrome 21

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...