0

I have two JSON files that I am trying to load in python

queue.json

[
    ["Person 1", "B"],
    ["Person 2", "C"],
    ["Person 3", "A"],
    ["Person 4", "B"],
    ["Person 5", "C"],
]

and stock.json

 {
   "A": 5, 
   "B": 3, 
   "C": 10
 }

I'm using this code to load in the stock file

 import json

 # Load the stock file.
 stock = json.load(open("stock.json"))

but when I use this code to load in the queue file it says that no JSON object could be coded:

 import json
 # Load the queue file.

 queue = json.load(open("queue.json"))
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135

2 Answers2

1

The problem is the content of the JSON file. Try changing the queue.json to

[
    {"B":"Person 1"},
    {"C":"Person 2"},
    {"A":"Person 3"},
    {"B":"Person 4"},
    {"C":"Person 5"}
]
Dean Christian Armada
  • 6,724
  • 9
  • 67
  • 116
  • the letters in stock are supposed to represent the medicine that person 1 2 3 4 and 5 are queuing for, i need to write a program that will give medicine to the people queuing and then update the amount of the stock file once it has gone through the list of people queuing and given them the medicine they need. the way the json file is set out is the way i have been given it in –  May 01 '17 at 03:59
  • I updated the answer, in that json, you can easily determine which medicine is used and just add a +1 on the stock for every queue finished – Dean Christian Armada May 01 '17 at 04:07
1

This is not valid json:

[
    ["Person 1", "B"],
    ["Person 2", "C"],
    ["Person 3", "A"],
    ["Person 4", "B"],
    ["Person 5", "C"],
]

The problem is the trailing comma on the list. One solution is to use yaml instead. yaml is a superset of json, and will accept the comma.

import yaml
queue = yaml.load(open(""queue.json"))
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • when i do that it comes up "ImportError: No module named yaml" –  May 01 '17 at 04:01
  • You will need to install yaml, if you want to use it. http://stackoverflow.com/questions/14261614/how-do-i-install-the-yaml-package-for-python#14262462 – Stephen Rauch May 01 '17 at 04:02