1

I want to import in my python program only some fields of a JSON file composed of lines of the following type:

{  
   "business_id":"Apn5Q_b6Nz61Tq4XzPdf9A",
   "name":"Minhas Micro Brewery",
   "neighborhood":"",
   "address":"1314 44 Avenue NE",
   "city":"Calgary",
   "state":"AB",
   "postal_code":"T2E 6L6",
   "latitude":51.0918130155,
   "longitude":-114.031674872,
   "stars":4.0,
   "review_count":24,
   "is_open":1,
   "attributes":{  
      "BikeParking":"False",
      "BusinessAcceptsCreditCards":"True",
      "BusinessParking":"{'garage': False, 'street': True, 'validated': False, 'lot': False, 'valet': False}",
      "GoodForKids":"True",
      "HasTV":"True",
      "NoiseLevel":"average",
      "OutdoorSeating":"False",
      "RestaurantsAttire":"casual",
      "RestaurantsDelivery":"False",
      "RestaurantsGoodForGroups":"True",
      "RestaurantsPriceRange2":"2",
      "RestaurantsReservations":"True",
      "RestaurantsTakeOut":"True"
   },
   "categories":"Tours, Breweries, Pizza, Restaurants, Food, Hotels & Travel",
   "hours":{  
      "Monday":"8:30-17:0",
      "Tuesday":"11:0-21:0",
      "Wednesday":"11:0-21:0",
      "Thursday":"11:0-21:0",
      "Friday":"11:0-21:0",
      "Saturday":"11:0-21:0"
   }
}

For example I would like to import only the fields: business_id, name and categories. I tried in different ways, but the program does not recognize the fields and each line is seen as a single field. For example, I have this problem using the following command:

x = pd.read_json('.../data.json')

I also tried to import it like this:

with open('.../data.json', 'r') as f:
    x = json.load(f)

When I try the command

x = x["business_id","name","categories"]

it returns the following error

KeyError: "['business_id' 'name' 'categories'] not in index"

The program does not recognize the fields in any way.

Matthew Purdon
  • 754
  • 11
  • 28
Luigi
  • 181
  • 3
  • 15

1 Answers1

0

I want to import in my python program only some fields of a JSON file composed of lines of the following type:

Not sure if I understood what you mean by 'import' a JSON file, but I assume you wanted to extract a subset of JSON file and save it as a local variable

If so, your code snippet using the default json module wasn't too far off. Json.load() function returns json data in Python dictionary. Knowing this, we just have to make small adjustment to how you extract the data from the python dictionary

Before we start, I saved your JSON file as 'data.json'

import json
with open(r'data.json') as jsonfile:
     data = json.load(jsonfile) # you had it right up to here
     data_import = [data['business_id'],data['name'],data['categories']] #creating local variable

Since you're already importing from a JSON/python dictionary, creating a python dictionary as a local imported variable is another good way to keep the data clean

import json
with open(r'data.json') as jsonfile:
     data = json.load(jsonfile)
     data_import = {}
     data_import['business_id'] = data['business_id']
     data_import['name'] = data['name']
     data_import['categories'] = data['categories']
Hannest
  • 206
  • 3
  • 5
  • Both ways you suggested, give the same error: "TypeError: list indices must be integers or slices, not str" – Luigi Jan 18 '19 at 19:49
  • That's strange. The second method doesn't even have a list. Have you tried to isolate the problem? Check which line the TypeError is coming from – Hannest Jan 18 '19 at 19:53
  • I'm sorry, I made a mistake. But it gives error too. Traceback (most recent call last): File "........", line 36, in data = json.load(jsonfile) File ".........", line 296, in load return loads(fp.read(), MemoryError – Luigi Jan 18 '19 at 20:20
  • I see that you got a MemoryError, it's likely that it's not a problem with your code, but your JSON file is pretty huge. check here, [link](https://stackoverflow.com/questions/40399933/memoryerror-when-loading-a-json-file). You might have to break down the jsonfile into smaller subsets – Hannest Jan 18 '19 at 20:25
  • I copied a hundred lines of lines from the file and saved them to another json file. I tried to load the new file, but it still gives errors. – Luigi Jan 18 '19 at 20:31
  • Traceback (most recent call last): File "...", line 32, in data = json.load(jsonfile) # you had it right up to here File "C:\Users\Luigi\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 299, in load parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) – Luigi Jan 18 '19 at 20:31
  • File "C:\Users\Luigi\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 354, in loads return _default_decoder.decode(s) File "C:\Users\Luigi\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 342, in decode raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 930) – Luigi Jan 18 '19 at 20:31
  • I think we have strayed away from the original question too much. I recommend posting another questions regarding your error outputs. It's difficult to read the messages in a comment – Hannest Jan 18 '19 at 20:41