'Outputting a Dataframe with an array variable in JSON format

I have the dataframe d in the following with two variables userid and segments, The variable segments is in an array and looks like this:

'f1eb2711a1ee16f535e9c7c93644948e': ['arts_and_culture',
  'arts_and_culture_books_mag',
  'entertainment',
  'entertainment_freq',
  'entertainment_entertainment_television_animation_cartoon'],
 'daa14f0904ca66161bd9be3ddeabda73': ['arts_and_culture',
  'entertainment',
  'entertainment_entertainment_television_animation_cartoon']}

I want to output the data into JSON format, however the following code is returning an error:

list2 = [{"userid":key,"segments":value} for key,value in d.items()]
with open(path_to_upload + 'segmentmembership.json', 'w+') as outfile:
    for i in list2:
        json.dump(i, outfile)
        outfile.write('\n')

AttributeError: 'list' object has no attribute 'items'


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source