{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import json\n", "import os" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def open_json(filename):\n", " with open(filename, 'r') as json_file:\n", " return json.load(json_file)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "def add_ids(json_file):\n", " id_counter = 1\n", " for post in json_file:\n", " post[\"id\"] = id_counter\n", " id_counter += 1\n", " if 'comments' in post:\n", " for comment in post['comments']:\n", " comment[\"id\"] = id_counter\n", " id_counter += 1\n", " if 'replies' in comment:\n", " for reply in comment['replies']:\n", " reply[\"id\"] = id_counter\n", " id_counter += 1\n", " if 'replies' in reply:\n", " for sec_reply in reply['replies']:\n", " sec_reply[\"id\"] = id_counter\n", " id_counter += 1\n", " return json_file" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "def create_json(filename, data):\n", " with open(filename, 'w', encoding = \"utf-8\", ) as file:\n", " json.dump(data, file, indent=4, separators=(',',': '))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "for json_file in os.listdir(\"json_data\"):\n", " data = open_json(f'json_data/{json_file}')\n", " data = add_ids(data)\n", " create_json(f'json_data_id/{json_file}', data)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "sentiment", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }