Home > OS >  How to print all messages available in json object received from github in python?
How to print all messages available in json object received from github in python?

Time:11-10

I have received a json object that shows comparison between two tags on github repo. I want to show all messages that are available inside the JSON object. The json object can be seen here: https://api.github.com/repos/git/git/compare/v2.37.4...v2.38.1

I have used the requests library available in python to receive the object:

import requests
response= requests.get('https://api.github.com/repos/git/git/compare/v2.37.4...v2.38.1')
json_obj=response.json()

Now how can I print the values of each instance of attribute named 'message' found in the json object?

CodePudding user response:

Here this will help

import requests
response= requests.get('https://api.github.com/repos/git/git/compare/v2.37.4...v2.38.1')
json_obj=response.json()
for i in json_obj.keys():
    print(i , json_obj[i])  #  there i is for the the key and json_obj[i] gives the value at the key
print(json_obj["base_commit"]['commit']['message']) # will print the message

CodePudding user response:

This prints the message of every commit in the JSON response.

import requests

response = requests.get('https://api.github.com/repos/git/git/compare/v2.37.4...v2.38.1')
json_obj = response.json()

for commit in json_obj['commits']:
    print(commit['commit']['message'])
  • Related