0

I have the following strings :

d1 = "[{'col 1':'a','col 2':'b'}]"

d2 = '[{"col 1":"a","col 2":"b"}]'

These strings contains the same data.

I only inverted the use of single and double quotes.

But for read_json they are not the same :

>>> pd.read_json(d1, orient='records')
# builtins.ValueError: Expected object or value

>>> pd.read_json(d2, orient='records')
#   col 1 col 2
# 0     a     b

I have done some unsuccessful research and I don't understand what's going on.

So I have done some tests :

>>> type(d1)
# <class 'str'>
>>> type(d2)
# <class 'str'>
>>> d1==d1
# True
>>> d2==d2
# True
>>> d1==d2
# False

So same type (str), same content but they are not equal. Why ?

Laurent B.
  • 1,653
  • 1
  • 7
  • 16
  • 4
    They are not the same because one of them is valid json and the other isn't. – tkausl Mar 11 '23 at 15:12
  • Notice the test `d1==d2`, the strings do have different content. To clarify what [tkausl](https://stackoverflow.com/users/1080064/tkausl) said, representing strings with single quotes like `'col 1'` is invalid json. Always use double quotes – Nikhil Devadiga Mar 11 '23 at 15:22
  • Maybe it's due to javascript strange behaviour like json format is born from JS – Laurent B. Mar 11 '23 at 15:31

0 Answers0