• Register
0 votes
10k 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 37 views
37 views
Problem: I've been trying to figure out a good way to load JSON objects in Python. I send this json data: {'http://example.org/about': {'http://purl.org/dc/terms/title': [{'type': 'literal', 'value': "Anna's Homepage"}]}} to the backend where it ... seems for me not that efficient since it accept any kind of data even the ones that are not in json format. Any suggestions will be much appreciated.
asked Feb 23 Mashhoodch 13k points
0 votes
1 answer 303 views
303 views
Problem: I am using google geocode API to test the following Python3.5 code but receive the error below. raise JSONDecodeError("Expecting value", s, err.value) from None >JSONDecodeError: Expecting value
asked Jan 26 Mashhoodch 13k points
0 votes
1 answer 661 views
661 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 450 views
450 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 5 views
5 views
Problem: how to I Solve this: valueerror: expecting property name: line 1 column 2 (char 1) HELP! I need help understanding this: valueerror: expecting property name: line 1 column 2 (char 1) Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 10 Mashhoodch 13k points
0 votes
0 answers 1 view
1 view
Problem: Can anyone anwer me and take off me from this trouble? Thanks in advance.
asked 8 hours ago niyatiry 2.9k points
0 votes
1 answer 2 views
2 views
Problem Please tell me the correct choice for the following question: How to overcome “DateTime.DateTime, not JSON serializable”?please explain the.Thanku
asked 2 days ago neeraj 9.5k points
0 votes
1 answer 2 views
2 views
Problem: Please help me to solve it out : no json object could be decoded python
asked 3 days ago rakib1 50.4k points
0 votes
1 answer 327 views
327 views
Problem: How to I Solve this: the json object must be str, bytes or bytearray, not 'dict' HELP! I need help understanding this: the json object must be str, bytes or bytearray, not 'dict' Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 6 Mashhoodch 13k points
0 votes
1 answer 12 views
12 views
Problem: My code creates a dictionary and I want it to write to my Json file where each dictionary will be in a new line. My dictionary: hostDict = {"key1": "val1", "key2": "val2", "key3": {"sub_key1": "sub_val2", "sub_key2": "sub_val2", "sub_key3": "sub_val3"}, "key4": "val4"} Part of my code: g = open('data.txt', 'a') with g as outfile: json.dump(hostDict, outfile)
asked Feb 25 Ashty 5.7k points