1

I am trying to get value of id i.e. "id": 59 which is in the curl output in form of json. Below is the curl output in json:

[{"id":59,"description":"This is a demo project","name":"Demo_Project","name_with_namespace":"sam / Demo_Project","path":"demo_project","path_with_namespace":"sam/demo_project","created_at":"2020-03-02T08:43:13.664Z","default_branch":"master","tag_list":[],"ssh_url_to_repo":"ssh://[email protected]:2222/sam/demo_project.git","http_url_to_repo":"https://od-test.od.com/gitlab/sam/demo_project.git","web_url":"https://od-test.od.com/gitlab/sam/demo_project","readme_url":"https://od-test.od.com/gitlab/sam/demo_project/blob/master/README.md","avatar_url":null,"star_count":0,"forks_count":0,"last_activity_at":"2020-04-09T09:28:09.860Z","namespace":{"id":2259,"name":"sam","path":"sam","kind":"user","full_path":"sam","parent_id":null,"avatar_url":"https://secure.gravatar.com/avatar/755db8ssqaq50dcc9d189c53523b?s=80\u0026d=identicon","web_url":"https://od-test.od.com/gitlab/sam"}}]

I am using python to parse the json and get the value of id. I have tried the following command to do the same but got an error.

curl --header "PRIVATE-TOKEN: 9999ayayayar66" "https://od-test.od.com/gitlab/api/v4/search?scope=projects&search=demo_project" | python -c 'import sys, json; print(json.load(sys.stdin)["id"])'

Error:

Error

Can anyone help me the correct python command to get the value of id. Thanks in advance.

2
  • Brilliant questions and you almost solved it yourself. I never thought of piping sys.stdin before. Thank you for that. Commented Apr 12, 2020 at 9:58
  • You welcome @PraysonW.Daniel Commented May 31, 2020 at 13:49

5 Answers 5

7

The JSON contains an array of objects but you are treating it like it is a single object:

import sys, json; print(json.load(sys.stdin)["id"])

You're basically saying "here is a collection of objects, give me the ID of the object". It doesn't make sense.

If you assume you only ever want the ID of the first object in the array, you can use this:

import sys, json; print(json.load(sys.stdin)[0]["id"])
Sign up to request clarification or add additional context in comments.

Comments

4

The root of you JSON object is actually an array, so you should get the first item out of it first:

curl --header "PRIVATE-TOKEN: 9999ayayayar66" "https://od-test.od.com/gitlab/api/v4/search?scope=projects&search=demo_project" | python -c 'import sys, json; print(json.load(sys.stdin)[0]["id"])'

Comments

3

You have to write print(json.load(sys.stdin)[0]["id"]) because json response is a list of dictionaries, not a dictionary.

Comments

1

Since you also tagged Python Requests, your CURL command could be translated to:

import requests

headers = {
    'PRIVATE-TOKEN': '9999ayayayar66',
}

params = (
    ('scope', 'projects'),
    ('search', 'demo_project'),
)

response = requests.get('https://od-test.od.com/gitlab/api/v4/search', headers=headers, params=params)

Then you can get the id value from the JSON Response Content with response.json()[0]["id"]. This uses the built-in JSON decoder to convert the JSON response to a list of dictionaries.

3 Comments

Requests is not part of python standard library and you may not always control/install it on-demand. Question here was about curl.
@Phil this question was tagged with python requests, you seemed to have missed that.
That's correct. I have not mentioned that and indeed, python Requests makes our life easier with json.
0
curl xxx | python -m json.tool

python provide a good json tool and can chained by pipeline

1 Comment

Please edit and add a little bit of explanation how this solves the problem stated in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.