in python json csv covert ~ read.
Handy converting of data in CSV into JSON

Handy converting of data in CSV into JSON

Currently i'm working on my fabricator.js library , which is basically a javascript analog of my already existing scala fabricator library. The reason i'm doing this is that i would like to use my fake data generator in node cli tool and also i would like to make a Chrome extension that would help manual testers to get random "close to reality looking" fake data. On one of the steps i faced with an issue of extracting table with data on html page and converting it into JSON file. The table looked like this :

If you select it and paste into the Spreadsheet, then you'll get a nice table with your data, but how can you convert it into JSON? Well, in my previous posts i already said that for small automation tasks i really love Python. As i expected, after 5 minutes of goodling i came to this solution :

  1. Save spreadsheet with data as csv file
  2. Open your favorite IDE or text editor and run this file :

This script contains data that is specific to my use case, but you can easily adjust it for you. Here's how :

  1. Modify csvfile and jsonfile variables with a path to your csv file and to where you want to save your json file
  2. Modify fieldnames array with column names that you have in your csv file

As a result, you'll have a nicely formatted json file with data that you need :

Happy coding.

comments powered by Disqus
comments powered by Disqus