Converting python tuple, lists, dictionaries containing pandas objects (series/dataframes) to json
<p>I know I can convert pandas object like <code>Series</code>, <code>DataFrame</code> to json as follows:</p>

<pre><code>series1 = pd.Series(np.random.randn(5), name='something')
jsonSeries1 = series1.to_json() #{"0":0.0548079371,"1":-0.9072821424,"2":1.3865642993,"3":-1.0609052074,"4":-3.3513341839}
</code></pre>

<p>However what should I do when that series is encapsulated inside other datastructure, say dictionary as follows:</p>

<pre><code>seriesmap = {"key1":pd.Series(np.random.randn(5), name='something')}
</code></pre>

<p>How do I convert above map to json like this:</p>

<pre><code>{"key1":{"0":0.0548079371,"1":-0.9072821424,"2":1.3865642993,"3":-1.0609052074,"4":-3.3513341839}}
</code></pre>

<p><code>simplejson</code> does not work:</p>

<pre><code> jsonObj = simplejson.dumps(seriesmap)
</code></pre>

<p>gives</p>

<pre><code>Traceback (most recent call last):
  File "C:\..\py2.py", line 86, in &lt;module&gt;
    jsonObj = json.dumps(seriesmap)
  File "C:\Mahesh\Program Files\WinPython-64bit-3.4.4.4Qt5\python-3.4.4.amd64\lib\site-packages\simplejson\__init__.py", line 380, in dumps
    return _default_encoder.encode(obj)
  File "C:\Mahesh\Program Files\WinPython-64bit-3.4.4.4Qt5\python-3.4.4.amd64\lib\site-packages\simplejson\encoder.py", line 275, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Mahesh\Program Files\WinPython-64bit-3.4.4.4Qt5\python-3.4.4.amd64\lib\site-packages\simplejson\encoder.py", line 357, in iterencode
    return _iterencode(o, 0)
  File "C:\Mahesh\Program Files\WinPython-64bit-3.4.4.4Qt5\python-3.4.4.amd64\lib\site-packages\simplejson\encoder.py", line 252, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: 0   -0.038824
1   -0.047297
2   -0.887672
3   -1.510238
4    0.900217
Name: something, dtype: float64 is not JSON serializable
</code></pre>

<p>To generalize this even further, I want to convert arbitrary object to json. The arbitrary object may be simple int, string or of complex types such that tuple, list, dictionary containing pandas objects along with other types. In dictionary the pandas object may lie at arbitrary depth as some key's value. I want to safely convert such structure to valid json. Is it possible?</p>

<p><strong>Update</strong></p>

<p>I just tried encapsulating DataFrame as a value of one of the keys of a dictionary and converting that dictionary to json by encapsulating in another DataFrame (as suggested in below answer). But seems that it does not work:</p>

<pre><code>import pandas as pd

d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
    'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)

mapDict = {"key1":df}
print(pd.DataFrame(mapDict).to_json())
</code></pre>

<p>This gave:</p>

<pre><code>Traceback (most recent call last):
  File "C:\Mahesh\repos\JavaPython\JavaPython\bin\py2.py", line 80, in &lt;module&gt;
    print(pd.DataFrame(mapDict).to_json())
  File "C:\Mahesh\Program Files\WinPython-64bit-3.4.4.4Qt5\python-3.4.4.amd64\lib\site-packages\pandas\core\frame.py", line 224, in __init__
    mgr = self._init_dict(data, index, columns, dtype=dtype)
  File "C:\Mahesh\Program Files\WinPython-64bit-3.4.4.4Qt5\python-3.4.4.amd64\lib\site-packages\pandas\core\frame.py", line 360, in _init_dict
    return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
  File "C:\Mahesh\Program Files\WinPython-64bit-3.4.4.4Qt5\python-3.4.4.amd64\lib\site-packages\pandas\core\frame.py", line 5231, in _arrays_to_mgr
    index = extract_index(arrays)
  File "C:\Mahesh\Program Files\WinPython-64bit-3.4.4.4Qt5\python-3.4.4.amd64\lib\site-packages\pandas\core\frame.py", line 5270, in extract_index
    raise ValueError('If using all scalar values, you must pass'
ValueError: If using all scalar values, you must pass an index
</code></pre>

<p>call <code>pd.DataFrame</code> on <code>seriesmap</code> then use <code>to_json</code></p>

<pre><code>pd.DataFrame(seriesmap).to_json()

'{"key1":{"0":0.8513342674,"1":-1.3357052602,"2":0.2102391775,"3":-0.5957492995,"4":0.2356552588}}'
</code></pre>

python json pandas