Create a Post
cancel
Showing results for 
Search instead for 
Did you mean: 
bhaizlett123
Contributor

Running JQ within python

I have a little python script that works great and i have this piece that runs an api command to get members from a group

client.api_call("show-group", {"name": "TestGrp"})

But i am trying to pass it to jq to filter out only the data I need which is just name and type. If i run it in the cli using mgmt_cli I can pass to jq no problem, but how do I do it in python using this type of api call?

 

6 Replies
PhoneBoy
Admin
Admin

Pretty sure there ways to parse JSON with native Python modules.
Think there is also a JQ module for Python.

bhaizlett123
Contributor

Thanks for your response.

I just tried using the jq library but everytime I try to load it into json it keeps giving me the error that it can't load an api response, is there a way to convert it to a json object so it can load successfully.

PhoneBoy
Admin
Admin

Our API only returns results in JSON.
I suspect it has to do with how you’re calling it in Python, but don’t know for certain.
Recommend looking at some working examples in the community.

0 Kudos
StuartGreen
Employee
Employee

Hi, If you can share the rest of the script I'd happily take a look (make sure you've edited out any sensitive data).

Generally, you would store the output of your API call into a variable:

my_results = client.api_call("show-group", {"name": "TestGrp"})

 

depending on what's returned, you might either have a string which contains JSON, or a Python object. If you're using an IDE like Visual Studio Code or PyCharm, you should be able to debug the script and add a breakpoint after this line to see the class of my_results. If it's a string, you can use "my_ object = json.loads(my_results)", if it's an object you can start using it however you like (again, the debug panel will help with what methods to use to interrogate the data).

Maik
Advisor

@StuartGreenbasically already explained it. However if you want to understand it in detail I can recommend you to check out https://www.w3schools.com/python/python_json.asp.

Basically, with "json.loads()" you can "import" json data into python to then query it like a list or dictionary.

Afterwards, if you want the json format back, you can use "json.dumps()" to re-create a json out of the filtered data - if required.

0 Kudos
Art_Zalenekas
Employee
Employee

To add to those great answers, I found IDLE of Python is great in loading values and checking the types (type()) of those variables so that you know what to expect. In addition to that, if you are using VS Code (if not I highly recommend it, it's free as well and runs on Windows/Linux/macOS), you can load great plugins for Python that on the fly will give you docs of the libraries plus the return functions and methods types (dict, list, object, etc.). It all depends on your setup of the IDE.

As you start manipulating data of JSON datasets, you will need to loop through the variables. Nested loops will be commonly expected data, as that's what you do with JQ as well. Take a look at:
hxxps://www.codespeedy.com/how-to-loop-through-json-with-subkeys-in-python/

I personally like to work with Python Comprehensions for if and for functions. Check out:
hxxps://realpython.com/list-comprehension-python/

Have fun!

Leaderboard

Epsum factorial non deposit quid pro quo hic escorol.

Upcoming Events

    CheckMates Events