Dev Jalla
Dev Jalla

Reputation: 2509

python json error Expecting property name: line 1 column 2 (char 1)

Please help

def checkActionType(jsondata):
    print("In checkActionType method") 
    print type(jsondata)
    jsonformat = json.loads(jsondata)
    action=str(jsonformat["action"])

and i passing

data = {u'userId': 3, u'module': u'report', u'clientId': 3, u'action': u'tablestats'}

r = checkActionType(data)

Getting an error

jsonformat = json.loads(jsondata)
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)

Upvotes: 2

Views: 6971

Answers (2)

Mohsen_Fatemi
Mohsen_Fatemi

Reputation: 3401

you can do it in two ways :

1 - define your data like a json , and pass json to method :

def checkActionType(jsondata):
    print("In checkActionType method") 
    print type(jsondata)
    action=str(jsondata["action"])

defining data :

data = {'userId': 3, 'module': 'report', 'clientId': 3, 'action': 'tablestats'}

using it :

r = checkActionType(data)

2 - pass the String instead :

def checkActionType(jsondata):
    print("In checkActionType method") 
    print type(jsondata)
    jsonformat = json.loads(jsondata)
    action=str(jsonformat["action"])

and pass this data instead :

data = "{'userId': 3, 'module': 'report', 'clientId': 3, 'action': 'tablestats'}"

call it :

r = checkActionType(data)

enter image description here

Upvotes: 0

Andriy Ivaneyko
Andriy Ivaneyko

Reputation: 22061

You data isn't a valid json which can be converted into dictionary, it's actually a dictinary, data shall be a valid json string, try to pass data below as a parameter:

data = '{"action": "tablestats", "userId": 3, "clientId": 3, "module": "report"}'

See more about json in the article "JSON: What It Is, How It Works, & How to Use It"

Upvotes: 1

Related Questions