• Register
0 votes
7k views

Problem :

I am trying to figure out the good way to load the JSON objects in the Python. I send below json data:

{'http://example.org/about': {'http://purl.org/dc/terms/title': [{'type': 'literal', 'value': "Anna's Homepage"}]}}

To my backend where it should be received as the string then I used the json.loads(data) to parse it.

But each time I got a same below exception :

“ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)”

I did lot of research on it but nothing seems to work for me. 

Any suggestions on my issue will be much appreciated.

6 5 3
7,540 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

The below one is not the correct JSON.

{'http://example.org/about': {'http://purl.org/dc/terms/title': [{'type': 'literal', 'value': "Anna's Homepage"}]}}


The below one is the correct JSON.

{"http://example.org/about": {"http://purl.org/dc/terms/title": [{"type": "literal", "value": "Anna's Homepage"}]}}

OR

I had the similar problem in the recent past. The two components while communicating with each other were using the queue .

The First component was not doing the json.dumps before putting the message to queue. So a JSON string generated by the receiving component was in the single quotes. This was causing the below error

Expecting property name enclosed in double quotes”

Adding the json.dumps started creating the correctly formatted JSON & solved my issue.

9 7 4
38,600 points
0 votes

Solution:

This:

{'http://example.org/about': {'http://purl.org/dc/terms/title': [{'type': 'literal', 'value': "Anna's Homepage"}]}}

is not JSON.
This:

{"http://example.org/about": {"http://purl.org/dc/terms/title": [{"type": "literal", "value": "Anna's Homepage"}]}}

is JSON.

EDIT:
JSON specification - RFC7159 explains that a string statrts and ends with quotation mark. That is ". Single quoute ' has no semantic interpretation in JSON and is approved just inside a string.

as JSON just approves enclosing strings with double quotes you can manipulate the string :

str = str.replace("\'", "\"")

This will replace all intances of single quote with double quote in the JSON string str.

You can also employ js-beautify which is less strict:

$ pip install jsbeautifier
$ js-beautify file.js

In my instance, double quotes was not a problem.

Last comma gave me similar error message.

{'a':{'b':c,}}
           ^

To dispel this comma, I wrote few easy code.

import json

with open('a.json','r') as f:
    s = f.read()
    s = s.replace('\t','')
    s = s.replace('\n','')
    s = s.replace(',}','}')
    s = s.replace(',]',']')
    data = json.loads(s)

I've inspected your JSON data

{'http://example.org/about': {'http://purl.org/dc/terms/title': [{'type': 'literal', 'value': "Anna's Homepage"}]}}

in http://jsonlint.com/ and the results :

Error: Parse error on line 1:
{   'http://example.org/
--^
Expecting 'STRING', '}', got 'undefined'

Tempering it to the following string resolve the JSON error:

{
    "http://example.org/about": {
        "http://purl.org/dc/terms/title": [{
            "type": "literal",
            "value": "Anna's Homepage"
        }]
    }
}

JSON strings should use double quotes. The JSON python library introduces this so you are unable to load your string. Your data requires to look like this:

{"http://example.org/about": {"http://purl.org/dc/terms/title": [{"type": "literal", "value": "Anna's Homepage"}]}}

In case that's not something you can practice, you could use ast.literal_eval() instead of json.loads()

As it definitely tells in error, names must be confined in double quotes instead of single quotes. The string you pass is only not a valid JSON. It must look like

{"http://example.org/about": {"http://purl.org/dc/terms/title": [{"type": "literal", "value": "Anna's Homepage"}]}}

I employed this process and managed to get the desired output. my script

x = "{'inner-temperature': 31.73, 'outer-temperature': 28.38, 'keys-value': 0}"

x = x.replace("'", '"')
j = json.loads(x)
print(j['keys-value'])

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 391 views
391 views
Problem : I am facing error “Expecting value: line 1 column 1 (char 0)” when trying to decode the JSON.The URL I use for a API call works fine in a browser, but gives above error when done through the curl request. The error happens at the return simplejson.loads(response_json)
asked Jan 20, 2020 jwilliam 3.9k points
0 votes
1 answer 139 views
139 views
Problem: I have fundamental knowledge on java , json and gson. I have below method: public static Object parseStringToObject(String json) {     String Object = json;     Gson gson = new Gson();     Object objects = gson.fromJson(object, Object. ... strange error message: "com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1"
asked Aug 4, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 28 views
28 views
I'm trying to read a json and get its values. I have a folder with the JSON's archives, and I need to open all archives and get the values from them. This is the code: # -*- encoding: utf-8 -*- from pprint import pprint import json import os def start(): for ... () print json_data This way I can see what the json files contain, but I can't use for example access by the key, like json_data['url']
asked Oct 24, 2020 psandprop 2.4k points
0 votes
1 answer 40 views
40 views
Can someone explain me why i am seeing this error that told me to use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $. How to solve it and what is reason behind this error?
asked Sep 14, 2020 Daniel Anderson 4k points
1 vote
1 answer 1.1K views
1.1K views
Problem : Can someone please tell me what is wrong with my code? I have tried almost everything and still unable to understand why my code keeps giving me below error: Parse error on line 3: ...", "shortName": “Simple”, "longN -------------------------------------- ... 1, "versionLabel": 1.7 , "watchapp": { "watchface": true }, "appKeys": { "dummy": 0 }, "resources": { "media": [] } }
asked Jan 2, 2020 alecxe 7.5k points
0 votes
1 answer 7 views
7 views
I was working on that. I just want to know how to do that?
asked Nov 30, 2020 TeamScript 9.2k points
0 votes
1 answer 7 views
7 views
I was working on that but I got into some problems.
asked Nov 30, 2020 TeamScript 9.2k points
0 votes
1 answer 18 views
18 views
I was working on that but I got into some problems.
asked Nov 30, 2020 TeamScript 9.2k points
0 votes
1 answer 9 views
9 views
I was working on that but I got into some problems.
asked Nov 30, 2020 TeamScript 9.2k points
0 votes
1 answer 9 views
9 views
I am new to this. I just want to know how to do that?
asked Nov 30, 2020 TeamScript 9.2k points