1

I have a large CSV file running up to 1000 lines of data, I would like to create a JSON file for each data block in CSV file.

Below is how the CSV file looks like. The first row is a header, rows 2 & 3 are the data pertaining to country_1 whereas for country_2 there is only data in row 4 enter image description here

copied here the content of CSV

countryname MultiValues_name    MulitValues_Value   dateRange_name  dataRange_start date_range_end
country_1   Variables   AAA selectdate  2019-01-01T00:00:00Z    2019-02-02T00:00:00Z
        BBB         
country_2   Variables       selectdate  1996-01-01T00:00:00Z    1996-02-02T00:00:00Z

I would like to store each data block in CSV as a JSON object in a separate file I am Expecting JSON Object for country_1 to look like below in a separate file lets say file1.json

{
  "countryname": "country_1",
  "MultiValues": [{"name": "variables", "value": ["AAA","BBB"]}],
  "dateRange": [{"name": "selectdate","start": "2019-01-01T00:00:00Z","end": "2019-02-02T00:00:00Z"}]
}

Similarly for country_2 stored as file2.json

{
  "countryname": "country_2",
  "dateRange": [{"name": "selectdate","start": "1996-01-01T00:00:00Z","end": "1996-02-02T00:00:00Z"}]
}

I am able to read from CSV and write to Json for simple name value pairs.In this case values are stored as arrays or values are stored as json objects which inturn has one array inside. This seems tricky .I have the privelage to edit the structure of CSV file if this might help to create the json in easier way

PS: I went through answers from How to convert CSV file to multiline JSON? but couldn't find the solution there

Butner
  • 81
  • 1
  • 9

1 Answers1

0

If you'd use pandas you could just to do this:

import pandas as pd 
df = pd.read_csv(<filename>)
df.to_json(<outfilename>)