KR29
KR29

Reputation: 433

Json parsing in python

I have a JSON string as a result of a query operation . I assign the result to a string literal and then try to read the string. I get the below error.

SyntaxError: EOL while scanning string literal

I get the result in the below format from a query operation with \n

str = {"start": 0,
  "time": "2015-Mar-15 17:04:33.197042 ::setup Initializing",
  "type": "solar",}

import json
json.loads(str)

Should I convert it into a docstring? I'm using python 3.4

Upvotes: 1

Views: 1276

Answers (2)

Forge
Forge

Reputation: 6834

In your code str is an object, not a string. To make it a JSON formatted string use:

json.dumps(str)

Upvotes: 1

chickahoona
chickahoona

Reputation: 2034

In your example "str" is already an object, so you dont have to parse it. It has already been parsed.

try this:

str =
{
    "start": 0,
    "time": "2015-Mar-15 17:04:33.197042 ::setup Initializing",
    "type": "solar",
}
print str["start"]

If you had something like this:

str = """
{
    "start": 0,
    "time": "2015-Mar-15 17:04:33.197042 ::setup Initializing",
    "type": "solar",
 }"""

you could do:

import json

json.loads(str)

Upvotes: 7

Related Questions