Skip to content

Unescaped quote marks in to_json output when run on multi indexed object #15288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
beegibson opened this issue Feb 2, 2017 · 1 comment
Closed
Labels
Bug Duplicate Report Duplicate issue or pull request IO JSON read_json, to_json, json_normalize

Comments

@beegibson
Copy link

Code Sample

import pandas as pd
import numpy as np

arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], ['one',
    'two', 'one', 'two', 'one', 'two', 'one', 'two']]

tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
series = pd.Series(np.random.randn(8), index=index)

print(series.to_json())

Output:

{"["bar","one"]":-0.0591166327,"["bar","two"]":0.871145093,"["baz","one"]":0.61280938,"["baz","two"]":0.3848564991,"["foo","one"]":-0.8986592304,"["foo","two"]":-0.4631529084,"["qux","one"]":-1.3482521044,"["qux","two"]":0.5209236198}

Problem description

The output contains unescaped quotation marks, which makes the output invalid json.

Expected Output

Quote marks should be escaped, for example:

{"[\"bar\",\"one\"]":-0.0591166327,"[\"bar\",\"two\"]":0.871145093,"[\"baz\",\"one\"]":0.61280938,"[\"baz\",\"two\"]":0.3848564991,"[\"foo\",\"one\"]":-0.8986592304,"[\"foo\",\"two\"]":-0.4631529084,"[\"qux\",\"one\"]":-1.3482521044,"[\"qux\",\"two\"]":0.5209236198}

Output of pd.show_versions()

I reproduced it in pandas install from master:

INSTALLED VERSIONS ------------------ commit: None python: 3.6.0.final.0 python-bits: 64 OS: Linux OS-release: 4.8.8-200.fc24.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_AU.UTF-8 LOCALE: en_AU.UTF-8

pandas: 0.19.0+401.g8452080
nose: None
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.12.0
scipy: None
statsmodels: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
pandas_datareader: None

@jreback jreback added Bug Duplicate Report Duplicate issue or pull request IO JSON read_json, to_json, json_normalize labels Feb 2, 2017
@jreback jreback added this to the No action milestone Feb 2, 2017
@jreback
Copy link
Contributor

jreback commented Feb 2, 2017

thanks this was just reported and is a duplicate of #15273

welcome to do a pull request to fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request IO JSON read_json, to_json, json_normalize
Projects
None yet
Development

No branches or pull requests

2 participants