Danny
Danny

Reputation: 510

Using Python to turn one JSON object/dict into two

I've got JSON data formatted like this.

  {
    "website": "http://www.zebrawebworks.com/zebra/bluetavern/day.cfm?&year=2018&month=6&day=29", 
    "date": "2018-06-29", 
    "headliner": [
      "Delta Ringnecks", 
      "Flathead String Band"
    ], 
    "data": [
      "4:00 PM", 
      "FEE:  $0", 
      "Jug Band Music", 
      "8:00 PM", 
      "FEE:  $5", 
      "Old Time Fiddle & Banjoby some young turks!"
    ]
  }

I'm working through a bunch of items that look like this in a for concert in data: loop. On dates where there are two concerts like this, I need to create a new Python dictionary, so that each concert is in its own dictionary like so:

  {
    "website": "http://www.zebrawebworks.com/zebra/bluetavern/day.cfm?&year=2018&month=6&day=29", 
    "date": "2018-06-29", 
    "headliner": "Delta Ringnecks",  
    "data": [
      "4:00 PM", 
      "FEE:  $0", 
      "Jug Band Music", 
    ]
  },
  {
    "website": "http://www.zebrawebworks.com/zebra/bluetavern/day.cfm?&year=2018&month=6&day=29", 
    "date": "2018-06-29", 
    "headliner": "Flathead String Band"
    "data": [
      "8:00 PM", 
      "FEE:  $5", 
      "Old Time Fiddle & Banjoby some young turks!"
    ]
  }

Is there a recommended way to do this? I can't change the data in the for loop itself, right?, because then it would screw up my iteration.

Could I append it to the end of data so that the for loop covers the new dictionaries (I would still need to parse some data after everything is separated)?

Or should I maybe create a new dictionary with the split days, delete the two-concerts-in-one-day objects, and then combine the dictionaries I have left?

I hope this is enough info and that I'm not mixing terminology too much. I'm very new to the JSON Python module and have been struggling with how to efficiently approach this problem. Thank you.

Upvotes: 0

Views: 51

Answers (3)

Randy
Randy

Reputation: 14847

You can get a pretty clean version of this using the grouper idiom from the itertools documentation:

In [42]: new_list = [{'website': d['website'], 'date': d['date'], 'headliner': headliner, 'data': list(datarow)}
    ...:             for headliner, datarow in zip(d['headliner'], grouper(d['data'], 3))]
    ...:

In [43]: new_list
Out[43]:
[{'website': 'http://www.zebrawebworks.com/zebra/bluetavern/day.cfm?&year=2018&month=6&day=29',
  'date': '2018-06-29',
  'headliner': 'Delta Ringnecks',
  'data': ['4:00 PM', 'FEE:  $0', 'Jug Band Music']},
 {'website': 'http://www.zebrawebworks.com/zebra/bluetavern/day.cfm?&year=2018&month=6&day=29',
  'date': '2018-06-29',
  'headliner': 'Flathead String Band',
  'data': ['8:00 PM',
   'FEE:  $5',
   'Old Time Fiddle & Banjoby some young turks!']}]

Upvotes: 1

Danny
Danny

Reputation: 510

Here's the solution I came up with thanks with the help of nosklo above. Hope it helps someone with a similar problem in the future.

new_concerts = []
    for concert in blue_data:

        if len(concert['headliner']) == 2:
            new_concert = concert.copy()
            new_concert['headliner'] = str(concert['headliner'][1])
            concert['headliner'] = str(concert['headliner'][0])
            mid = len(concert['data']) / 2
            new_concert['data'] = concert['data'][mid:]
            concert['data'] = concert['data'][0:mid]
            new_concerts.append(new_concert)

    blue_data = blue_data + new_concerts

Upvotes: 0

nosklo
nosklo

Reputation: 222852

I suggest you create copies of the dict and store the specific data in each one. For example:

result = [] 

for pos in range(0, len(original_dict['headliner'])):
     new_dict = original_dict.copy()
     new_dict['data'] = original_dict['data'][pos*3:(pos+1)*3]
     new_dict['headliner'] = original_dict['headliner'][pos]
     result.append(new_dict)

print(result)

Upvotes: 1

Related Questions