83 lines
2.1 KiB
Plaintext
83 lines
2.1 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 119,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"import json\n",
|
|
"from pandas.io.json import json_normalize\n",
|
|
"\n",
|
|
"with open('./data/freeformatter-out.json',encoding='utf-8') as json_file:\n",
|
|
" data = json.load(json_file)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 104,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def flatten_json(nested_json, exclude=['']):\n",
|
|
" \"\"\"Flatten json object with nested keys into a single level.\n",
|
|
" Args:\n",
|
|
" nested_json: A nested json object.\n",
|
|
" exclude: Keys to exclude from output.\n",
|
|
" Returns:\n",
|
|
" The flattened json object if successful, None otherwise.\n",
|
|
" \"\"\"\n",
|
|
" out = {}\n",
|
|
"\n",
|
|
" def flatten(x, name='', exclude=exclude):\n",
|
|
" if type(x) is dict:\n",
|
|
" for a in x:\n",
|
|
" if a not in exclude: flatten(x[a], name + a + '_')\n",
|
|
" elif type(x) is list:\n",
|
|
" i = 0\n",
|
|
" for a in x:\n",
|
|
" flatten(a, name + str(i) + '_')\n",
|
|
" i += 1\n",
|
|
" else:\n",
|
|
" out[name[:-1]] = x\n",
|
|
"\n",
|
|
" flatten(nested_json)\n",
|
|
" return out\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 118,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"#flat_json = flatten_json(data,['postmeta'])\n",
|
|
"flat_json = pd.DataFrame([flatten_json(x) for x in data['channel']['item']])\n",
|
|
"flat_json.to_csv('./freeform.csv')"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "'Python Interactive'",
|
|
"language": "python",
|
|
"name": "17501b92-bb2a-435d-aa93-fcc8fde404f5"
|
|
},
|
|
"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.7.0"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|