0

I'm trying figure out how to load file called foo.json with this content:

[
    {
        "bar1":  "foobar1-1",
        "bar2":  "foobar1-2",
        "bar3":  "foobar1-3"
    },
    {
        "bar1":  "foobar2-1",
        "bar2":  "foobar2-2",
        "bar3":  "foobar2-3"
    }
]

Code I've tried:

 with open('foo.json') as data_file:    
    print(data_file)
    data = json.load(data_file)
    print(data)

Everything I've seen parsing json with json.load seems to be geared toward dictionary content but this json represents an array of objects or an array of dictionaries. Json.load does not seem to even work with a array of dictionaries. Perhaps I need to use a different json parsing library for this. Any help is appreciated.

Jason Thurston
  • 347
  • 5
  • 7
  • What issue are you encountering? I don't know what you mean by "everything in json.load seems to be geared toward dictionary content"... but the `json` module handles any valid JSON just fine. – juanpa.arrivillaga Aug 02 '17 at 21:49
  • 1
    Aaand the problem is *this isn't valid JSON* because of the extra comma. Try copy-pasting it into https://jsonlint.com/ and clicking validate. – juanpa.arrivillaga Aug 02 '17 at 21:51
  • I'm sorry, I tried to create a simplified version of my json data for this post and I accidentally left in the comma. I've edited them out in the post. – Jason Thurston Aug 02 '17 at 21:56
  • Ok, but now this loads just fine. Again **what exactly is the issue you are encountering**? You've simply vaguely stated "Json.load does not seem to even work with a array of dictionaries." but that is **definitely not true**. – juanpa.arrivillaga Aug 02 '17 at 21:59
  • Indeed, I've put your example data into a file, and used your exact code and it runs as expected... – juanpa.arrivillaga Aug 02 '17 at 22:02
  • Also, please consider that "it doesn't work" [really isn't an adequate problem specification](https://meta.stackexchange.com/questions/147616/what-do-you-mean-it-doesnt-work) – juanpa.arrivillaga Aug 02 '17 at 22:04

2 Answers2

0

The json file is not valid, delete the last comma (',') in each dictionay. after which it will work fine :)

[
    {
        "bar1":  "foobar1-1",
        "bar2":  "foobar1-2",
        "bar3":  "foobar1-3"
    },
    {
        "bar1":  "foobar2-1",
        "bar2":  "foobar2-2",
        "bar3":  "foobar2-3"
    }
]
Dror Av.
  • 1,184
  • 5
  • 14
0

Wow this is embarrassing. I just copied and pasted my code from this post to double check it and it worked. I inspected the new file and saw that it is ASCII. The original file that I've had a problem with for 2 days is UTF-16. I converted the UTF-16 to ASCII and it json.load works! So it's a problem with the file type. I guess I just have to figure out how to get the json.load to understand UTF-16 files!

Jason Thurston
  • 347
  • 5
  • 7
  • `open('whatever.json', encoding='utf-16')` or maybe `'utf-16-le'` or `'utf-16-be'`, but you should always know your encoding. If you actually included the error in your post, that would have been a clue, but everyone was left to guess. – Nick T Aug 02 '17 at 22:25
  • Sorry, this is my 1st python program. I barely know how to use the Visual Studio 2017 IDE, the error seemed so ominous that I thought it had no value Plus the DOS pop-up windows is a pain to copy and paste from.. BTW, the IDE doesn't show "encoding" as a parameter when it shows me options as I start typing a comma and then "e" but I added it anyway and it worked! Thank You! – Jason Thurston Aug 02 '17 at 23:44