• Register
0 votes
5.3k 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

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 301 views
301 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 jwilliam 3.9k points
0 votes
1 answer 54 views
54 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 Raphael Pacheco 4.9k points
0 votes
1 answer 5 views
5 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 5 days ago psandprop 2.3k points
0 votes
1 answer 12 views
12 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 Daniel Anderson 4k points
0 votes
1 answer 4 views
4 views
Hello all! I’m newbie programmer. Currently I’m working with jSON to fetch some data for my new project.But I’ve stucked with some problems. I’m trying using getJSON method to get value of it. Need help. Any help will be appreciated. What I want to get is jobDescription’s value. {"JobData":{"JobDescription":"Cashier Wanted","JobSearch":"Search"}}
asked 2 days ago mrain 120 points