Home > Net >  How do I pull objects from a JSON file using python?
How do I pull objects from a JSON file using python?

Time:07-28

Currently using VSCode with Jupyter Notebook, I'm using a get request to a HTTP URL, I'm able to get to the data and print it in the console, how do I extract the object I want and print them?

import requests
import json
requests.urllib3.disable_warnings()

## API Request to test server ##
response_API = requests.get('https://test.com:8080/exports', auth=('test', 'test1234'),  verify=False)

##Printing of JSON ##
pretty_response = json.dumps(response_API.json(), indent=4)
print(pretty_response)

When I print I can see all the JSON information, how do I extract the fields I want and print? r.json doesn't work in VSCode for me for some reason. I need clients and path printed. Sample tidbit below.

    {
        "digest": "1234121234",
        "exports": [
            {
    
                "clients": [
                    "tester1.com",
                    "tester2.deere.com"
       ],
                "paths": [
                    "/home/test"
                ],
}

CodePudding user response:

when you use json.dumps() it tries to use a python dictionary and dumps it as a JSON object, or when you have a json string object, you can use json.loads() to load it as a python dictionary, in this case you just need to print response_API.json()

data = (response_API.json())
print(data["digest"]

the result must be:
"1234121234"

  • Related