Home > OS >  How to write CSV data to a different key in a JSON file based on a condition?
How to write CSV data to a different key in a JSON file based on a condition?

Time:01-19

i have convert csv json but the one of the json field has be based on condition.

so here i have to generate json for each row of csv entery i have the code for csv to json but i'm unable to understand about link column my link of csv should be place to json with condition such as first it has to guess the extension of link and according to the extension the data of link should be written to json. suppose extension is mp3 so in json it should be placed under the link under the song filed i will paste some json file for understanding expected output

my csv

id  |   name        |   add         |   link             
1   |   rohit       |   delhi       | https:\\\\tumera.mp3
2   |   rushi       |   delhi       | https:\\\\webpage1.html
3   |   tenz        |   delhi       | https:\\\\flower.png
4   |   pal     |   delhi       | https:\\\\exam.html

i have code:

with this i'm able to convert csv to json

import csv
import json
csv_file =  open("data.csv",'r')
csv_reader = csv.DictReader(csv_file, None)
for row in csv_reader:
    row[id]=int(row[id])
    json.dumps(row, sort_keys=True,default = str,indent=2)
    jsonfile=json.dumps(row,indent=2)
    jsonoutput = open(row['id'] '.json','w')
    jsonoutput.write(jsonfile)
    jsonoutput.close()
csv_file.close()

my expected json:

so my link field is totally based on condition

i just want get values of link according to extension if it is mp3 it shoul be written in song

if it is png/jpg it should be written in images

if it is html it should be written in pages

i tried doing it by using if else statement but it didn't work

1.json:

{
    "id":"1"
    "name":"rohit"
    "add":delhi
    "link":{
    "song":[{"https:\\song.mp3"}]
      } 
}

2.json

{
    "id":"2"
    "name":"rushi"
    "add":"delhi"
    "link":{
    "page":[{"https:\\webpage1.html"}]
      }
}

3.json

{
    "id":"3"
    "name":"tenz"
    "add":"delhi"
    "link":{
    "image":[{"https:\\flower.png"}]
     }
}

4.json

{
    "id":"4"
    "name":"pal"
    "add":"delhi"
    "link":{
    "page":[{"https:\\exam.html"}]
     }
}

CodePudding user response:

UPDATE

Tested with data.

Data:

cat data.csv
id,name,add,link
1,rohit,delhi,https://tumera.mp3
2,rushi,delhi,https://webpage1.html
3,tenz,delhi,https://flower.png
4,pal,delhi,https://exam.html

import csv
import json
ext_dict = {'jpg': 'image', 'png': 'image', 'html': 'page', 'mp3': 'song'}

with open("data.csv",'r') as csv_file:
   csv_reader = csv.DictReader(csv_file, None)
   for row in csv_reader:
       link_ext = row['link'].split('.')[-1]
       row['link'] = {ext_dict[link_ext]: [row['link']]}
       jsonfile=json.dumps(row,indent=2)
       with open(row['id'] '.json','w') as jsonoutput:
           jsonoutput.write(jsonfile)

Using the context manager with means the file is closed automatically for you.

Output:

 cat 1.json
{
  "id": "1",
  "name": "rohit",
  "add": "delhi",
  "link": {
    "song": [
      "https://tumera.mp3"
    ]
  }
}

 cat 2.json
{
  "id": "2",
  "name": "rushi",
  "add": "delhi",
  "link": {
    "page": [
      "https://webpage1.html"
    ]
  }
}

cat 3.json
{
  "id": "3",
  "name": "tenz",
  "add": "delhi",
  "link": {
    "image": [
      "https://flower.png"
    ]
  }
}

 cat 4.json
{
  "id": "4",
  "name": "pal",
  "add": "delhi",
  "link": {
    "page": [
      "https://exam.html"
    ]
  }
}



  • Related