{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "JSON Objects into Dictionary python.ipynb", "provenance": [], "authorship_tag": "ABX9TyPyEK9HuextIxIRW5JP6uGk", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "<a href=\"https://colab.research.google.com/github/annanya-mathur/annanya-mathur/blob/main/JSON_Objects_into_Dictionary_python.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" ] }, { "cell_type": "code", "metadata": { "id": "kaiAD5BIhrBJ" }, "source": [ "import json as js\n", "import pprint as pp" ], "execution_count": 3, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "hu7bSR8giEOO" }, "source": [ "json_string= '{\"id\": 2140,\"title\": \"gj\",\"description\": \"ghj\",\"location\": \"Hermannplatz 5-6, 10967 Berlin, Germany\",\"lng\": 0,\"lat\": 0,\"userId\": 4051,\"name\": \"manoj\",\"commentCount\": 0}'" ], "execution_count": 9, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "KyBwiCletICo" }, "source": [ "# Converting json objects to dictionary\n", "dict= js.loads(json_string)" ], "execution_count": 12, "outputs": [] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "o2X2mtZQtJqZ", "outputId": "2fd31b00-6565-4796-e6e1-b41e366ac268" }, "source": [ "type(dict)" ], "execution_count": 13, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "dict" ] }, "metadata": { "tags": [] }, "execution_count": 13 } ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "X_2bl6_otKTx", "outputId": "3a059906-8b9d-40f7-87a3-bdfcc131ca48" }, "source": [ "dict" ], "execution_count": 14, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'commentCount': 0,\n", " 'description': 'ghj',\n", " 'id': 2140,\n", " 'lat': 0,\n", " 'lng': 0,\n", " 'location': 'Hermannplatz 5-6, 10967 Berlin, Germany',\n", " 'name': 'manoj',\n", " 'title': 'gj',\n", " 'userId': 4051}" ] }, "metadata": { "tags": [] }, "execution_count": 14 } ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "TYSN4bxjwcBK", "outputId": "cfa9a722-67ff-4240-9184-49d5f63385ae" }, "source": [ "# directly converting json file to dictionary\n", "with open (\"/content/sample.json\") as sample:\n", " dict=js.load(sample)\n", "dict" ], "execution_count": 22, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'commentCount': 0,\n", " 'description': 'ghj',\n", " 'id': 2140,\n", " 'lat': 0,\n", " 'lng': 0,\n", " 'location': 'Hermannplatz 5-6, 10967 Berlin, Germany',\n", " 'name': 'manoj',\n", " 'title': 'gj',\n", " 'userId': 4051}" ] }, "metadata": { "tags": [] }, "execution_count": 22 } ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "sQwQRg9hzFlV", "outputId": "f2ed6989-3c59-43c0-f5e6-cfcf8ed9b6ad" }, "source": [ "type(dict)" ], "execution_count": 23, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "dict" ] }, "metadata": { "tags": [] }, "execution_count": 23 } ] } ] }