<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Running JQ within python in API / CLI Discussion</title>
    <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Running-JQ-within-python/m-p/103444#M5346</link>
    <description>&lt;P&gt;Thanks for your response.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description>
    <pubDate>Thu, 26 Nov 2020 14:16:43 GMT</pubDate>
    <dc:creator>bhaizlett123</dc:creator>
    <dc:date>2020-11-26T14:16:43Z</dc:date>
    <item>
      <title>Running JQ within python</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Running-JQ-within-python/m-p/103365#M5344</link>
      <description>&lt;P&gt;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&lt;/P&gt;&lt;PRE&gt;client.api_call(&lt;SPAN&gt;"show-group"&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;{&lt;SPAN&gt;"name"&lt;/SPAN&gt;: "TestGrp"})&lt;/PRE&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Nov 2020 01:20:02 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/Running-JQ-within-python/m-p/103365#M5344</guid>
      <dc:creator>bhaizlett123</dc:creator>
      <dc:date>2020-11-26T01:20:02Z</dc:date>
    </item>
    <item>
      <title>Re: Running JQ within python</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Running-JQ-within-python/m-p/103376#M5345</link>
      <description>&lt;P&gt;Pretty sure there ways to parse JSON with native Python modules.&lt;BR /&gt;Think there is also a JQ module for Python.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Nov 2020 03:54:48 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/Running-JQ-within-python/m-p/103376#M5345</guid>
      <dc:creator>PhoneBoy</dc:creator>
      <dc:date>2020-11-26T03:54:48Z</dc:date>
    </item>
    <item>
      <title>Re: Running JQ within python</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Running-JQ-within-python/m-p/103444#M5346</link>
      <description>&lt;P&gt;Thanks for your response.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Nov 2020 14:16:43 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/Running-JQ-within-python/m-p/103444#M5346</guid>
      <dc:creator>bhaizlett123</dc:creator>
      <dc:date>2020-11-26T14:16:43Z</dc:date>
    </item>
    <item>
      <title>Re: Running JQ within python</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Running-JQ-within-python/m-p/103610#M5353</link>
      <description>&lt;P&gt;Our API only returns results in JSON.&lt;BR /&gt;I suspect it has to do with how you’re calling it&amp;nbsp;in Python, but don’t know for certain.&lt;BR /&gt;Recommend looking at some working examples in the community.&lt;/P&gt;</description>
      <pubDate>Sun, 29 Nov 2020 18:06:10 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/Running-JQ-within-python/m-p/103610#M5353</guid>
      <dc:creator>PhoneBoy</dc:creator>
      <dc:date>2020-11-29T18:06:10Z</dc:date>
    </item>
    <item>
      <title>Re: Running JQ within python</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Running-JQ-within-python/m-p/103679#M5355</link>
      <description>&lt;P&gt;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).&lt;/P&gt;
&lt;P&gt;Generally, you would store the output of your API call into a variable:&lt;/P&gt;
&lt;PRE&gt;&lt;STRONG&gt;my_results&lt;/STRONG&gt; = client.api_call(&lt;SPAN&gt;"show-group"&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;{&lt;SPAN&gt;"name"&lt;/SPAN&gt;: "TestGrp"})&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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).&lt;/P&gt;</description>
      <pubDate>Mon, 30 Nov 2020 10:32:32 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/Running-JQ-within-python/m-p/103679#M5355</guid>
      <dc:creator>StuartGreen</dc:creator>
      <dc:date>2020-11-30T10:32:32Z</dc:date>
    </item>
    <item>
      <title>Re: Running JQ within python</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Running-JQ-within-python/m-p/103682#M5356</link>
      <description>&lt;P&gt;&lt;a href="https://community.checkpoint.com/t5/user/viewprofilepage/user-id/12391"&gt;@StuartGreen&lt;/a&gt;basically already explained it. However if you want to understand it in detail I can recommend you to check out &lt;A href="https://www.w3schools.com/python/python_json.asp" target="_blank"&gt;https://www.w3schools.com/python/python_json.asp&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;Basically, with "json.loads()" you can "import" json data into python to then query it like a list or dictionary.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Nov 2020 10:52:24 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/Running-JQ-within-python/m-p/103682#M5356</guid>
      <dc:creator>Maik</dc:creator>
      <dc:date>2020-11-30T10:52:24Z</dc:date>
    </item>
    <item>
      <title>Re: Running JQ within python</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Running-JQ-within-python/m-p/103763#M5357</link>
      <description>&lt;P&gt;To add to those great answers, I found IDLE of Python is great in loading values and checking the types (&lt;EM&gt;type()&lt;/EM&gt;)&amp;nbsp;of those variables so that you know what to expect. In addition to that, if you are using &lt;STRONG&gt;VS Code&lt;/STRONG&gt; (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.&lt;/P&gt;
&lt;P&gt;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:&lt;BR /&gt;hxxps://&lt;A href="http://www.codespeedy.com/how-to-loop-through-json-with-subkeys-in-python/" target="_blank"&gt;www.codespeedy.com/how-to-loop-through-json-with-subkeys-in-python/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;I personally like to work with Python Comprehensions for&amp;nbsp;&lt;EM&gt;if&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;and&amp;nbsp;&lt;EM&gt;for&lt;/EM&gt; functions. Check out:&lt;BR /&gt;hxxps://realpython.com/list-comprehension-python/&lt;/P&gt;
&lt;P&gt;Have fun!&lt;/P&gt;</description>
      <pubDate>Mon, 30 Nov 2020 15:40:09 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/Running-JQ-within-python/m-p/103763#M5357</guid>
      <dc:creator>Art_Zalenekas</dc:creator>
      <dc:date>2020-11-30T15:40:09Z</dc:date>
    </item>
  </channel>
</rss>

