Home » Python » How to combine data from both a .json and .list file in an alternating fashion using python to dump into new JSON file?-Exceptionshub

How to combine data from both a .json and .list file in an alternating fashion using python to dump into new JSON file?-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

Essentially, my task is to extract information first from a JSON file and then from a .list file. This information is then in an alternating sequence to be dumped into a new JSON file called summary.json.

My approach so far is to extract all the json data to one list and then the list data to another list. From there, I add that information all 1 index at a time to a new combined list which is to be dumped.

# append json_data to list1
json_data = []
for i in json_files:
    with open(i) as f:
        json_data.append(json.load(f))

# append table_data to list2
table_data = []
for i in table_files:
    with open(i) as f:
        for line in f:
            table_data.append(line)

#format data to remove \n characters
table_data = [sub.replace('\n', '') for sub in table_data]

#combine list info
combined = []
while True:
    try:
        combined.append(json_data.pop(0))
        combined.append(table_data.pop(0))
    except IndexError:
        break

# dump list info to new json file
with open('summary.json', 'w') as f:
    json.dump(combined, f, indent=2)

Here is the output with my current code:

[
  {
    "json_object1_item1": "value1",
    "json_object1_item2": "value2",
    "json_object1_item3": "value3"
  },
  tablelist1_item1,
  {
    "json_object2_item1": "value1",
    "json_object2_item2": "value2",
    "json_object2_item3": "value3"
  }
 tablelist1_item2
]

The desired output is something like:

[
  {
    "json_object1_item1": "value1",
    "json_object1_item2": "value2",
    "json_object1_item3": "value3"
  },
  tablelist1_item1,
  tablelist1_item2,
  tablelist1_item3,
  {
    "json_object2_item1": "value1",
    "json_object2_item2": "value2",
    "json_object2_item3": "value3"
  }
 tablelist2_item1,
 tablelist2_item2,
 tablelist2_item3
]

How can I fix my code to get what I need? Is there a better approach to achieving my output? Thanks

How to&Answers:

The reason you are only getting one value from the table_data is because you have flattened it, e.g. [1,2,3,4,5,6] instead of [[1,2],[3,4],[5,6]], e.g.:

table_data = []
for i in table_files:
    with open(i) as f:
        table_data.append([line.rstrip('\n') for line in f])

And then you need to flatten it in your combine. The use of extend flattens the table_data. zip() is an easier way of combining two lists and will stop when one or other list is exhausted:

combined = []
for j, t in zip(json_data, table_data):
    combined.append(j)
    combined.extend(t)