{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## JSON files\n",
    "\n",
    "- It is common to store data in a file in a format that is easy to read, both for people and machines.\n",
    "- One such format is JSON.\n",
    "- This has many uses, but a common one is to store user variables that control how a code is run, without having to change the code itself, only the input file.\n",
    "- JSON files make use of key-value pairs, like dictionaries.\n",
    "    - values can be numbers, strings, arrays, true/false, null, or dictionaries (called objects), also null.\n",
    "    - Don't forget to end lines with commas, except for the last element in an object.\n",
    "- Here's a very short [tutorial](https://learnxinyminutes.com/json/)\n",
    "\n",
    "### Example file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "\u001b[33m\"Ldomain\"\u001b[39m:  \u001b[32m0.01\u001b[39m,\n",
      "\u001b[33m\"Li\"\u001b[39m:       \u001b[32m0.01\u001b[39m,\n",
      "\u001b[33m\"k\"\u001b[39m:        \u001b[32m1.0\u001b[39m,\n",
      "\u001b[33m\"nu\"\u001b[39m:       \u001b[32m1.5E-5\u001b[39m,\n",
      "\u001b[33m\"tRun\"\u001b[39m:     \u001b[32m0.1\u001b[39m,\n",
      "\u001b[33m\"N_eta\"\u001b[39m:    \u001b[32m10.76\u001b[39m,\n",
      "\u001b[33m\"C_lambda\"\u001b[39m: \u001b[32m15\u001b[39m,\n",
      "\u001b[33m\"dtFactor\"\u001b[39m: \u001b[32m0.5\u001b[39m,\n",
      "\u001b[33m\"dtWrite\"\u001b[39m:  \u001b[32m0.001\u001b[39m,\n",
      "\u001b[33m\"doRxn\"\u001b[39m:    false,\n",
      "\u001b[33m\"Tbounds\"\u001b[39m:  [\u001b[32m300\u001b[39m, \u001b[32m2500\u001b[39m],\n",
      "\u001b[33m\"mech\"\u001b[39m:     null,\n",
      "\n",
      "\u001b[33m\"thermo\"\u001b[39m: {\n",
      "    \u001b[33m\"mech\"\u001b[39m: \u001b[33m\"gri30.yaml\"\u001b[39m,\n",
      "    \u001b[33m\"P\"\u001b[39m:    \u001b[32m101325\u001b[39m,\n",
      "    \u001b[33m\"T0\"\u001b[39m:   \u001b[32m300\u001b[39m,\n",
      "    \u001b[33m\"X0\"\u001b[39m:   \u001b[33m\"O2:1, N2:7.52\"\u001b[39m,\n",
      "    \u001b[33m\"T1\"\u001b[39m:   \u001b[32m300\u001b[39m,\n",
      "    \u001b[33m\"X1\"\u001b[39m:   \u001b[33m\"CH4:1\"\u001b[39m\n",
      "},\n",
      "\n",
      "\u001b[33m\"Vars\"\u001b[39m: {\n",
      "    \u001b[33m\"scal\"\u001b[39m: {\n",
      "        \u001b[33m\"D\"\u001b[39m: \u001b[32m2.24E-5\u001b[39m,\n",
      "        \u001b[33m\"BC\"\u001b[39m: {\n",
      "            \u001b[33m\"loType\"\u001b[39m: \u001b[33m\"D\"\u001b[39m,\n",
      "            \u001b[33m\"hiType\"\u001b[39m: \u001b[33m\"D\"\u001b[39m,\n",
      "            \u001b[33m\"lo\"\u001b[39m:     \u001b[32m0.0\u001b[39m,\n",
      "            \u001b[33m\"hi\"\u001b[39m:     \u001b[32m1.0\u001b[39m\n",
      "        }\n",
      "    },\n",
      "    \u001b[33m\"ygas\"\u001b[39m: {\n",
      "        \u001b[33m\"BC\"\u001b[39m: {\n",
      "            \u001b[33m\"loType\"\u001b[39m: \u001b[33m\"D\"\u001b[39m,\n",
      "            \u001b[33m\"hiType\"\u001b[39m: \u001b[33m\"D\"\u001b[39m\n",
      "        }\n",
      "    },\n",
      "    \u001b[33m\"enth\"\u001b[39m: {\n",
      "        \u001b[33m\"BC\"\u001b[39m: {\n",
      "            \u001b[33m\"loType\"\u001b[39m: \u001b[33m\"D\"\u001b[39m,\n",
      "            \u001b[33m\"hiType\"\u001b[39m: \u001b[33m\"D\"\u001b[39m\n",
      "        }\n",
      "    }\n",
      "}\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "%pycat input.json"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read data from a JSON file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "with open(\"input.json\", \"r\") as ifile:\n",
    "    data = json.load(ifile)\n",
    "\n",
    "data.keys()\n",
    "\n",
    "# access data in the file using dictionary syntax:\n",
    "\n",
    "Ldomain  = data[\"Ldomain\"]\n",
    "rxns     = data[\"doRxn\"]\n",
    "scalBClo = data[\"Vars\"][\"scal\"][\"BC\"][\"lo\"]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "base",
   "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.14.7"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
