0

I'm new to python. I'm running python on Azure data bricks. I have a .json file. I'm putting the important fields of the json file here

{
"school": [
    {
        "schoolid": "mr1",
        "board": "cbse",
        "principal": "akseal",
        "schoolName": "dps",
        "schoolCategory": "UNKNOWN",
        "schoolType": "UNKNOWN",
        "city": "mumbai",
        "sixhour": true,
        "weighting": 3,
        "paymentMethods": [
            "cash",
            "cheque"
        ],
        "contactDetails": [
            {
                "name": "picsa",
                "type": "studentactivities",
                "information": [
                    {
                        "type": "PHONE",
                        "detail": "+917597980"
                    }
                ]
            }
        ],
        "addressLocations": [
            {
                "locationType": "School",
                "address": {
                    "countryCode": "IN",
                    "city": "Mumbai",
                    "zipCode": "400061",
                    "street": "Madh",
                    "buildingNumber": "80"
                },
                "Location": {
                    "latitude": 49.313885,
                    "longitude": 72.877426
                },

I need to create a data frame with schoolName as one column & latitude & longitude are others two columns. Can you please suggest me how to do that?

newinPython
  • 313
  • 1
  • 6
  • 19
  • 2
    Welcome. I'm sure that this question have been answered dozens of times before, try to use search. – Olvin Roght Jul 07 '20 at 16:19
  • That's a simple dict access. What have you tried so far? – AKX Jul 07 '20 at 16:19
  • you can use the json library to read the file and pandas to create the dataframe. Have you tried to google "how to read json in python" and "how to create a dataframe in python"? – Hoxha Alban Jul 07 '20 at 16:22
  • Does this answer your question? [How to parse JSON in Python?](https://stackoverflow.com/questions/7771011/how-to-parse-json-in-python) – Roshin Raphel Jul 07 '20 at 16:24
  • Does this answer your question? [Python read JSON file and modify](https://stackoverflow.com/questions/21035762/python-read-json-file-and-modify) – Hugo Jul 07 '20 at 16:45

2 Answers2

0

you can use the method json.load(), here's an example:

import json

with open('path_to_file/file.json') as f:
    data = json.load(f)

print(data)
Hugo
  • 93
  • 1
  • 8
  • Sorry, It's not solving my purpose. I can able to read the data using json.load. But I'm not getting how to retrieve data from 3rd level tag – newinPython Jul 07 '20 at 17:25
  • you mean you can't access the elements inside data? this might help you https://stackoverflow.com/questions/16129652/accessing-json-elements/16129667 – Hugo Jul 07 '20 at 17:29
  • 1
    Hi Hugo, your link helped. I'm able to do the task – newinPython Jul 08 '20 at 10:04
0

use this

import json # built-in
with open("filename.json", 'r') as jsonFile:
    Data = jsonFile.load()

Data is now a dictionary of the contents exp.

for i in Data:
    # loops through keys
    print(Data[i]) # prints the value

For more on JSON:

https://docs.python.org/3/library/json.html

and python dictionaries:

https://www.programiz.com/python-programming/dictionary#:~:text=Python%20dictionary%20is%20an%20unordered,when%20the%20key%20is%20known.

12ksins
  • 307
  • 1
  • 12