1

I'm using the answer here to convert my csv to json in python .

How to convert CSV file to multiline JSON?

Although each json field is unique . I'm not sure how to ensure how each sub json field has a list of unique elements.

Ex:

fieldnames = ("domains","ip_address")
reader = csv.DictReader( csvfile, fieldnames)
for row in reader:
    json.dump(row, jsonfile)
     jsonfile.write('\n')

Basically I want to ensure all the items in the ip_address list are unique. In case of the list shown below :

{"ip_address":"x,y,x"}

It should be :

{"ip_address":"x,y"}

I know how to obtain unique values from a list. I want to know how to access this list while performing the json dump.

Utkarsh.K
  • 37
  • 1
  • 7

1 Answers1

0

Try this:

my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', ...]

unique_list = []
for word in my_list:
    if word not in unique_list:
        unique_list.append(word)

Hope it helps THanks

Avinash Singh
  • 4,970
  • 8
  • 20
  • 35