Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I extract specific value from JSON response?

Hi I am trying to get value from JSON response which Im getting from GitLab API. Code should extract "commiter_name" value but it does not work, already tried several options from internet.

def getCom():

      com = requests.get("https://gitlab.com/api/v4/projects/.."
      
      headers = {'PRIVATE-TOKEN': '.....'}).content

Error:

File "getData.py", line 24, in getCom resp = com.json() ["commiter_name"]
AttributeError: 'bytes' object has no attribute 'json'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is because you did not include you api token in the header. Also please check that the website you are pointing to in the request ends in commits.

Do this,

headers = {'PRIVATE-TOKEN': 'Your API key here!'}
com = requests.get('https://gitlab.example.com/api/v4/projects/5/repository/commits', headers=headers)
print(com.json())

You can also try this with the output you got,

json.loads(com.decode('utf-8'))

You output should be a list of json data. You will be able to access it using a for loop to go through the list and finding your committer_name. For example if it was the first item in the json file.

com.json()[0]["commiter_name"]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading