Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I asked in another question for the type of this object, it is a text file coming from a URL.

variable = [["1","arbitrary string","another arbitrary string"],
["2","arbitrary string","another arbitrary string"],
["3","arbitrary string","another arbitrary string"],
["4","arbitrary string","another arbitrary string"]];
another_variable = "arbitrary string";

I was told it is a JSON object, but when I try json.loads, I get an error saying that "No JSON object could be decoded"

What am I missing please.

share|improve this question
1  
Is this a string? Can you show us what you're attempting exactly? – mgilson 2 hours ago
I have a text file coming from a URL, I need to parse this file to extract specific fields from the file :) – ammoun 2 hours ago
@ammoun What you have there is a python object, not a json string. I expect that the text file will be a string, so json.loads should work then. – quamrana 2 hours ago
@quamrana json.loads('variable = [["1","arbitrary string","another arbitrary string"],["2","arbitrary string","another arbitrary string"]["3","arbitrary string","another arbitrary string"],["4","arbitrary string","another arbitrary string"]];another_variable = "arbitrary string";') Still showing errors. – ammoun 2 hours ago
3  
This is not a JSON object. It seems like a pure Javascript file - you can execute it, but you can't 'deserialize' it. – Daniel Roseman 1 hour ago
show 5 more comments

3 Answers

json.loads works on a string. It decodes a JSON encoded string into a Python object. What you have here is a Python object, which you can encode to JSON with json.dumps. Also, there's no variable assignment within JSON. The only thing you can represent is a plain object.

share|improve this answer
Tried it with no luck. – ammoun 1 hour ago

Daniel Roseman is correct. This is not a JSON string. Just make sure to include commas in between each element of the list (you left one out).

variable = [["1","arbitrary string","another arbitrary string"],["2","arbitrary string","another arbitrary string"],["3","arbitrary string","another arbitrary string"],["4","arbitrary string","another arbitrary string"]]

variable
[[u'1', u'arbitrary string', u'another arbitrary string'],
[u'2', u'arbitrary string', u'another arbitrary string'],
[u'3', u'arbitrary string', u'another arbitrary string'],
[u'4', u'arbitrary string', u'another arbitrary string']]

another_variable = "arbitrary string"

another_variable
u'arbitrary string'
share|improve this answer
Still no luck... – ammoun 1 hour ago
note that you were missing a comma in the first assignment: ]["3", should have been ],["3", – D Bro 1 hour ago

The string which you get is not JSON (as claimed before), but partially it could be interpreted as JSON (right part of = statement). You could try to write simple parser to extract what is interesant to you. I had played around and got this:

import json
json_str = """
variable = [["1","arbitrary string","another arbitrary string"],
["2","arbitrary string","another arbitrary string"],
["3","arbitrary string","another arbitrary string"],
["4","arbitrary string","another arbitrary string"]];
another_variable = "arbitrary string";
"""

json_str_list = [js.strip().split("=")[1] for js in json_str.split(";") if js.strip()]
print("=preprocessed: %r" % json_str_list)
print("=json decoded: %r" % [json.loads(js) for js in json_str_list])

output is:

=preprocessed: [' [["1","arbitrary string","another arbitrary string"],\n["2","arbitrary string","another arbitrary string"],\n["3","arbitrary string","another arbitrary string"],\n["4","arbitrary string","another arbitrary string"]]', ' "arbitrary string"']

=json decoded: 
 [
 [[u'1', u'arbitrary string', u'another arbitrary string'], 
 [u'2', u'arbitrary string', u'another arbitrary string'], 
 [u'3', u'arbitrary string', u'another arbitrary string'], 
 [u'4', u'arbitrary string', u'another arbitrary string']], 

 u'arbitrary string']
share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.