Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am trying to parse some text files containing JSON objects in Python using the json.load() method. It's working for one set of them, but for this one it will not:

{
"mapinfolist":{
  "mapinfo":[
  {"sku":"00028-0059","price":"38.35","percent":"50","basepercent":"50","exact":0,"match":0,"roundup":0}
  ,{"sku":"77826-7230","price":"4.18","percent":"60","basepercent":"60","exact":1,"match":0,"roundup":0}
  ,{"sku":"77827-1310","price":"2.36","percent":"60","basepercent":"60","exact":1,"match":0,"roundup":0}
  ,{"sku":"77827-2020","price":"2.36","percent":"60","basepercent":"60","exact":1,"match":0,"roundup":0}
  ,{"sku":"77827-3360","price":"2.36","percent":"60","basepercent":"60","exact":1,"match":0,"roundup":0}
  ,{"sku":"77827-4060","price":"2.36","percent":"60","basepercent":"60","exact":1,"match":0,"roundup":0}
  ,{"sku":"77827-4510","price":"2.36","percent":"60","basepercent":"60","exact":1,"match":0,"roundup":0}
  ,{"sku":"77827-7230","price":"2.36","percent":"60","basepercent":"60","exact":1,"match":0,"roundup":0}
  ],
  "count":2
}
}

It is in a file called 'map.txt' - I open it using open('map.txt') and then call json.load(). When I run my test program (test.py), the following error trace is generated:

Traceback (most recent call last):
  File "test.py", line 28, in <module>
    main()
  File "test.py", line 23, in main
    map_list = json.load(f1) 
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/__init__.py", line 318, in loads
return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/decoder.py", line 361, in raw_decode
raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

The JSON object is valid - when I put it into https://www.jsoneditoronline.org/ it is parsed and displayed correctly, so I am having trouble identifying what could be stopping it from working when I try to do it in Python. Any advice would be much appreciated. Thanks!

EDIT: Here's my code.

import json
def main():

with open('map.txt') as f1:
    map_list = json.load(f1)

Trying map_list = json.loads(f1.read()) also does not work and gives me an almost identical error trace.

EDIT - RESOLVED:

I just copied and pasted FROM map.txt into a new TextEdit file map2.txt and used the new file instead, and it works now. I copied directly from the old file and made no changes - the only difference is that it is a different file. I can't make heads or tails of why that would be - any ideas? I would like to understand what may have happened so I can avoid the problem in the future.

share|improve this question
4  
Show us the code! – John Jun 18 '15 at 18:18
    
@John it has been added! – shinytinsmile Jun 18 '15 at 18:28
    
If I copy your JSON content into a file and load it with json.load(file('blah.txt')) in Python 2.7 it works fine. If I copy it into ideone.com and use Python 3 and json.loads(""" blah blah """) it works. Weird. I have to guess it's either referencing the wrong file (typo in the name) or the file was saved in a weird editor / encoding that makes it be interpreted differently? – TessellatingHeckler Jun 18 '15 at 18:30
    
@TessellatingHeckler I saved it in TextEdit and made sure it was plain text. The filename is definitely correct as well. :( – shinytinsmile Jun 18 '15 at 18:39
1  
It's a point. I notice in the error it says MAP_list = json.load(f2) and in your code sample it's called f1 - any chance you're opening the right file name but then passing json.load a different file variable? – TessellatingHeckler Jun 18 '15 at 18:44

Does the following solution work for you?

import json
f = open("map.txt")
map = json.loads(f.read())

Python Docs

share|improve this answer
    
No. Practically identical error trace. – shinytinsmile Jun 18 '15 at 18:33
    
It was probably an unusual white space ('\r\n') or ('\t') perhaps. Often times opening a file in emacs can highlight these unusual white spaces that can get injected by some windows applications. – Matt Jun 18 '15 at 22:11

maybe try to read all the file to string and then use json.loads

         def yourfunc():
             file = open('map.txt')
             json_string = file.read()
             map = json.loads(json_string)
share|improve this answer
    
This isn't really an answer. Some sample code would be more helpful – heinst Jun 18 '15 at 18:19

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.