1

I have not worked with JSON files so far, I have to resolve a requirement of filtering some data from a huge JSON file with lots of information Q1. should I change this JSON file into Pandas data frame and filter the desired information will it not be a bad coding technique Q2 how to filter data directly from JSON file in python

first few lines of JSON file actually the file is 30 times more of code and can not be posted here but filtering this very code will give me intuition to withdraw and store information

      {
      "response":{
    "status_code":200,
    "data":{
        "id":18288,
        "code":"u2vx",
        "accepts_instructions":true,
        "address":"76 Z block commercial market Phase-3 DHA Lahore.",
        "address_line2":"76 Z block commercial market Phase-3 DHA Lahore.",
        "budget":3,
        "chain":{
            "id":2,
            "is_accepting_global_vouchers":true,
            "main_vendor_code":"s0ox",
            "main_vendor_id":1439,
            "name":"KFC",
            "url_key":"kfc",
            "code":"cs5rw"
        },
        "city":{
            "id":200253,
            "name":"Lahore",
            "url_key":"lahore",
            "is_top_city":false,
            "is_main_city":false,
            "is_express_delivery_enabled":false,
            "latitude":31.510849,
            "longitude":74.37538147,
            "timezone":"Asia/Karachi"
        },
        "cuisines":[
            {
                "id":86,
                "name":"Fast Food",
                "url_key":"fast-food",
                "main":true
            }
        ],
        "custom_location_url":"",
        "customer_phone":"+3202440571, 0423-5746464, 0423-5692414, Mr Babar\t0301-4147257, 
           0332-1494447, 0332-1494448",
        "customer_type":"all",
        "delivery_box":"",
        "delivery_conditions":[
            {
                "delivery_fee":0,
                "delivery_fee_type":"amount",
                "minimum_order_amount":0,
                "maximum_order_amount":0
            }
        ],
        "delivery_fee_type":"amount",
        "delivery_fee_source":"disco",
        "delivery_provider_id":0,
        "description":"",
        "discounts":[
            
        ],
        "distance":8533.565,
        "experiments":[
            {
                "experiment_id":"recommendation-products-elements",
                "experiment_variation":"Control",
                "is_participating":false
            },
            {
                "experiment_id":"vendor-details-availability-concurrent",
                "experiment_variation":"Control",
                "is_participating":false
            },
            {
                "experiment_id":"recommendation-products-quantity",
                "experiment_variation":"Control",
                "is_participating":false
            }
        ],
        "food_characteristics":[
            {
                "id":120,
                "name":"Halal",
                "is_halal":true,
                "is_vegetarian":false
            },
            {
                "id":129,
                "name":"foodpanda delivery",
                "is_halal":true,
                "is_vegetarian":false
            }
        ],
        "favorite_data":null,
        "has_delivery_provider":true,
        "hero_image":"https://images.deliveryhero.io/image/fd-pk/LH/u2vx-hero.jpg",
        "hero_listing_image":"https://images.deliveryhero.io/image/fd-pk/LH/u2vx-listing.jpg",
        "is_active":true,
        "is_busy":false,
        "is_best_in_city":false,
        "is_checkout_comment_enabled":true,
        "is_delivery_enabled":true,
        "is_express_delivery_available":false,
        "is_express_delivery_enabled":false,
        "is_new":false,
        "is_new_until":"2019-09-07T00:00:00Z",
        "is_pickup_enabled":false,
        "is_premium":false,
        "premium_position":0,
        "is_preorder_enabled":false,
        "is_promoted":false,
        "is_replacement_dish_enabled":false,
        "is_service_fee_enabled":false,
        "is_service_tax_enabled":false,
        "is_service_tax_visible":false,
        "is_test":false,
        "is_vat_disabled":false,
        "is_vat_included_in_product_price":true,
        "is_vat_visible":false,
        "is_vat_included":true,
        "is_voucher_enabled":false,
        "latitude":31.473286,
        "location_event":null,
        "logo":"",
        "longitude":74.378387,
        "loyalty_percentage_amount":0,
        "loyalty_program_enabled":false,
        "maximum_express_order_amount":0,
        "menus":[
            {
                "id":35895,
                "code":"",
                "name":"Mid Night Menu",
                "description":"",
                "type":"delivery",
                "opening_time":"00:01:00",
                "closing_time":"06:00:00",
                "menu_categories":[
                    {
                        "id":284921,
                        "code":"s6ey-mc-5gy",
                        "name":"Midnight Deals",
                        "description":"(Starting From 12:00am)",
                        "products":[
                            {
                                "id":2148024,
                                "code":"s6ey-pr-owmq",
                                "name":"Midnight Deal 1",
                                "description":"Zinger burger with regular soft drink",
                                "display_price":"",
                                "master_category_id":8,
                                "file_path":"https://images.deliveryhero.io/image/fd- 
                                   pk/Products/1164992.png?width=%s",
                                "logo_path":"https://images.deliveryhero.io/image/fd- 
                               pk/Products/1164992.png?width=%s",
                                "images_urls":[
                                    
                                ],
                                "images":[
                                    {
                                        "image_url":"https://images.deliveryhero.io/image/fd- 
                            pk/Products/1164992.png",
                                        "tags":{
                                            
                                        }
                                    }
                                ],
                                "is_prepacked_item":false,
                    


      
ahmed
  • 11
  • 1
  • 6
  • 1
    Does this answer your question? [JSON to pandas DataFrame](https://stackoverflow.com/questions/21104592/json-to-pandas-dataframe) – Moradnejad Jun 26 '21 at 11:07
  • response data {'id': 18288, 'code': 'u2vx', 'accepts_instruc... status_code 200 – ahmed Jun 26 '21 at 17:27
  • No, the solution is not up to the mark, the output is neither readable nor editable no segregation of keys and values – ahmed Jun 27 '21 at 06:09

2 Answers2

0
import pandas as pd
df = pd.read_json(r'directory\Name.json')
df.head()
  • response data {'id': 18288, 'code': 'u2vx', 'accepts_instruc... status_code 200 //is all that i get as output can not do any operations , need to drop some columns – ahmed Jun 26 '21 at 17:28
0

If your data is unstructured, you have to open your file using json, like this:

JSON to pandas DataFrame

Otherwise, if it is already in a tabular structure, use Nikunj's answer, like this:

import pandas as pd
df = pd.read_json(r'directory\Name.json')
df.head()
Moradnejad
  • 3,466
  • 2
  • 30
  • 52