How to import fields from a JSON file?

时间:2019-01-18 18:18:27

标签: json python-3.x

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.

1 个答案:

答案 0 :(得分: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']