Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# `JSONASOBJ`\n",
    "An extension to the python `json` library that represents the JSON as a first class python object rather than a straight dictionary. Contents can still be accessed using dictionary format.\n",
    "\n",
    "\n",
    "## Introduction\n",
    "\n",
    "This is an extension to the python *json* library that represents the JSON\n",
    "as a first class python object rather than a straight \n",
    "dictionary.  Contents can still be accessed using dictionary format.\n",
    "\n",
    "---\n",
    "## Requirements\n",
    "\n",
    "* Python (3.0 or later)\n",
    "\n",
    "## Installation\n",
    "\n",
    "```bash\n",
    "pip install jsonasobj\n",
    "```\n",
    "\n",
    "## Short Example\n",
    "--------------\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Markus Lanthaler\n",
      "Markus Lanthaler\n",
      "Dave Longley\n",
      "http://xmlns.com/foaf/0.1/name\n",
      "{\n",
      "   \"@context\": {\n",
      "      \"name\": \"http://xmlns.com/foaf/0.1/name\",\n",
      "      \"knows\": \"http://xmlns.com/foaf/0.1/knows\",\n",
      "      \"menu\": {\n",
      "         \"@id\": \"name:foo\",\n",
      "         \"@type\": \"@id\"\n",
      "      }\n",
      "   },\n",
      "   \"@id\": \"http://me.markus-lanthaler.com/\",\n",
      "   \"name\": \"Markus Lanthaler\",\n",
      "   \"knows\": [\n",
      "      {\n",
      "         \"name\": \"Dave Longley\",\n",
      "         \"modelDate\": \"01/01/2015\",\n",
      "         \"extra\": {\n",
      "            \"age\": 17\n",
      "         }\n",
      "      },\n",
      "      {\n",
      "         \"name\": \"Barack Obama\"\n",
      "      }\n",
      "   ]\n",
      "}\n",
      "{'@context': {'knows': 'http://xmlns.com/foaf/0.1/knows',\n",
      "              'menu': {'@id': 'name:foo', '@type': '@id'},\n",
      "              'name': 'http://xmlns.com/foaf/0.1/name'},\n",
      " '@id': 'http://me.markus-lanthaler.com/',\n",
      " 'knows': [{'extra': {'age': 17},\n",
      "            'modelDate': '01/01/2015',\n",
      "            'name': 'Dave Longley'},\n",
      "           {'name': 'Barack Obama'}],\n",
      " 'name': 'Markus Lanthaler'}\n"
     ]
    }
   ],
   "source": [
    "import jsonasobj\n",
    "from pprint import PrettyPrinter\n",
    "pp = PrettyPrinter().pprint\n",
    "\n",
    "test_json = \"\"\"{\n",
    "  \"@context\": {\n",
    "    \"name\": \"http://xmlns.com/foaf/0.1/name\",\n",
    "    \"knows\": \"http://xmlns.com/foaf/0.1/knows\",\n",
    "    \"menu\": {\n",
    "      \"@id\": \"name:foo\",\n",
    "      \"@type\": \"@id\"\n",
    "    }\n",
    "  },\n",
    "  \"@id\": \"http://me.markus-lanthaler.com/\",\n",
    "  \"name\": \"Markus Lanthaler\",\n",
    "  \"knows\": [\n",
    "    {\n",
    "      \"name\": \"Dave Longley\",\n",
    "      \"menu\": \"something\",\n",
    "      \"modelDate\" : \"01/01/2015\"\n",
    "    }\n",
    "  ]\n",
    "}\"\"\"\n",
    "\n",
    "py_obj = jsonasobj.loads(test_json)\n",
    "py_obj.knows[0].extra = {'age': 17}\n",
    "py_obj.knows.append(dict(name='Barack Obama'))\n",
    "del py_obj.knows[0]['menu']\n",
    "print(py_obj.name)\n",
    "print(py_obj['name'])\n",
    "print(py_obj.knows[0].name)\n",
    "print(py_obj['@context'].name)\n",
    "print(jsonasobj.as_json(py_obj))\n",
    "pp(jsonasobj.as_dict(py_obj))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}