<?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: Json format from Cobra/ACI-toolkit query in Controllers</title>
    <link>https://community.cisco.com/t5/controllers/json-format-from-cobra-aci-toolkit-query/m-p/4067622#M100</link>
    <description>&lt;P&gt;Thanks Claudia, I did run into that output using the Toolkit and with your input now I'm able to dissect that entirely, very helpful.&lt;/P&gt;&lt;P&gt;Thanks a million.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Massimo&lt;/P&gt;</description>
    <pubDate>Thu, 16 Apr 2020 22:02:13 GMT</pubDate>
    <dc:creator>m.papini</dc:creator>
    <dc:date>2020-04-16T22:02:13Z</dc:date>
    <item>
      <title>Json format from Cobra/ACI-toolkit query</title>
      <link>https://community.cisco.com/t5/controllers/json-format-from-cobra-aci-toolkit-query/m-p/4064848#M96</link>
      <description>&lt;P&gt;Hi&amp;nbsp; there,&lt;/P&gt;&lt;P&gt;I'm digging my way through the queries with Cobra SDK and I'm getting stuck on how to extract a Json formatted output from a query. This is easily done when you push a new config using &lt;STRONG&gt;toJSONStr &lt;/STRONG&gt;from &lt;STRONG&gt;jsoncodec &lt;/STRONG&gt;in both cobra and ACI-toolkit but I'm not sure on how this can be done when you query the actual config on a fabric. My purpose is to be able to automate Json formatted config download from a certain number of elements/tenants without using the usual GUI right click "save as". This is my query sample with Cobra (ACI-toolkit produces similar situation):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;import cobra.mit.access
import cobra.mit.request
import cobra.mit.session
import cobra.model.fv
import cobra.model.pol
#from cobra.internal.codec.xmlcodec import toXMLStr
from cobra.internal.codec.jsoncodec import toJSONStr
from credentials import *

# set login session
session = cobra.mit.session.LoginSession(URL, LOGIN, PASSWORD)
# set object directory
moDir = cobra.mit.access.MoDirectory(session)
# submit login
moDir.login()
# filter class
cq = cobra.mit.request.ClassQuery('fvTenant')
# set subtree to full
cq.subtree = 'full'
# set property filter to tenant name
cq.propFilter = 'eq(fvTenant.name, "My_Tenant")'
# submit query
tnt = moDir.query(cq)
#print
print(toJSONStr(tnt))&lt;/PRE&gt;&lt;P&gt;Which gives the following output:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;AttributeError: 'listWithTotalCount' object has no attribute 'meta'&lt;BR /&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;I take it the code is obviously wrong. I've been trying with json.dumps as well for the sake of it but the outcome it is simply an empty []&lt;/P&gt;&lt;P&gt;Any hint would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 13 Apr 2020 18:36:32 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/json-format-from-cobra-aci-toolkit-query/m-p/4064848#M96</guid>
      <dc:creator>m.papini</dc:creator>
      <dc:date>2020-04-13T18:36:32Z</dc:date>
    </item>
    <item>
      <title>Re: Json format from Cobra/ACI-toolkit query</title>
      <link>https://community.cisco.com/t5/controllers/json-format-from-cobra-aci-toolkit-query/m-p/4065185#M97</link>
      <description>&lt;PRE&gt;#print
print(toJSONStr(tnt))&lt;BR /&gt;&lt;BR /&gt;---------------&lt;BR /&gt;Checking the objects around, it seems this is a generator of the MO objects, that we could use the iteration to list MO object.&lt;BR /&gt;I tried the following and it seems to work.&lt;BR /&gt;&lt;BR /&gt;print([json.loads(toJSONStr(mo)) for mo in tnt])&lt;BR /&gt;or &lt;BR /&gt;for mo in tnt:&lt;BR /&gt;    print(json.loads(toJSONStr(mo))&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Apr 2020 06:39:50 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/json-format-from-cobra-aci-toolkit-query/m-p/4065185#M97</guid>
      <dc:creator>Derek Zhang</dc:creator>
      <dc:date>2020-04-14T06:39:50Z</dc:date>
    </item>
    <item>
      <title>Re: Json format from Cobra/ACI-toolkit query</title>
      <link>https://community.cisco.com/t5/controllers/json-format-from-cobra-aci-toolkit-query/m-p/4065493#M98</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/202482"&gt;@m.papini&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Typically, the query will return an object.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, with the ACI-Toolkit a tenant query like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;# Download all of the tenants
&amp;nbsp; &amp;nbsp;tenants = ACI.Tenant.get(session)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;gets you a &lt;STRONG&gt;list&lt;/STRONG&gt; of Tenant objects like&amp;nbsp;&amp;lt;acitoolkit.acitoolkit.Tenant object at 0x10bae8c18&amp;gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;print the tenant variable which is of type &amp;lt;class 'list'&amp;gt;
[&amp;lt;acitoolkit.acitoolkit.Tenant object at 0x10bae8c18&amp;gt;, &amp;lt;acitoolkit.acitoolkit.Tenant object at 0x10c0ef128&amp;gt;, &amp;lt;acitoolkit.acitoolkit.Tenant object at 0x10c0ef160&amp;gt;, &amp;lt;acitoolkit.acitoolkit.Tenant object at 0x10c0d3828&amp;gt;, &amp;lt;acitoolkit.acitoolkit.Tenant object at 0x10c0d37f0&amp;gt;, &amp;lt;acitoolkit.acitoolkit.Tenant object at 0x10c0d37b8&amp;gt;, &amp;lt;acitoolkit.acitoolkit.Tenant object at 0x10c0d3780&amp;gt;, &amp;lt;acitoolkit.acitoolkit.Tenant object at 0x10c0d3748&amp;gt;, &amp;lt;acitoolkit.acitoolkit.Tenant object at 0x10c0a9320&amp;gt;, &amp;lt;acitoolkit.acitoolkit.Tenant object at 0x10c0a9390&amp;gt;, &amp;lt;acitoolkit.acitoolkit.Tenant object at 0x10c0ef198&amp;gt;, &amp;lt;acitoolkit.acitoolkit.Tenant object at 0x10c0ef1d0&amp;gt;]&lt;/PRE&gt;&lt;P&gt;Each of those Tenant objects has the methods below (one of which is .name). &amp;nbsp;To do this you can just use the dir method on the object. &amp;nbsp;Because you get back a list used the first element in the list (the first tenant object) and ran it through dir.&lt;/P&gt;&lt;PRE&gt;print(dir(response_list[0]))&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;lets see methods with dir:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_add_relation', '_attachments', '_check_attachment', '_check_relation', '_children', '_dedup_searchables', '_define_searchables', '_deleted', '_extract_relationships', '_generate_attributes', '_get_all_detached_relation', '_get_all_relation', '_get_all_relations_by_class', '_get_any_relation', '_get_apic_classes', '_get_children_classes', '_get_children_concrete_classes', '_get_instance_subscription_urls', '_get_name_dn_delimiters', '_get_name_from_dn', '_get_parent_class', '_get_parent_dn', '_get_parent_from_dn', '_get_starting_name_delimiter', '_get_subscription_urls', '_get_toolkit_to_apic_classmap', '_get_url_extension', '_has_any_relation', '_has_relation', '_instance_get_event', '_instance_get_subtree_faults', '_instance_has_events', '_instance_has_subtree_faults', '_instance_subscribe', '_instance_unsubscribe', '_parent', '_populate_from_attributes', '_relations', '_remove_all_relation', '_remove_attachment', '_remove_relation', '_session', '_tags', 'add_child', 'add_tag', 'attach', 'check_session', 'delete_tag', 'descr', 'detach', 'dn', 'exists', 'find', 'get', 'get_all_attached', 'get_all_attachments', 'get_attributes', 'get_child', 'get_children', 'get_deep', 'get_deep_apic_classes', 'get_dn_from_attributes', 'get_event', 'get_fault', 'get_from_json', 'get_interfaces', 'get_json', 'get_parent', 'get_searchable', 'get_table', 'get_tags', 'get_url', 'has_attachment', 'has_child', 'has_detachment', 'has_events', 'has_parent', 'has_tag', 'has_tags', 'info', 'infoList', 'is_attached', 'is_deleted', 'is_detached', 'is_interface', 'mark_as_deleted', 'mask_class_from_graphs', 'name', 'populate_children', 'push_to_apic', 'remove_child', 'remove_tag', 'set_parent', 'subscribe', 'subscribe_to_fault_instances_subtree', 'unsubscribe', 'update_db']&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note that one of the methods is “get_json”&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;    print(f"\nlets use the get_json method on one of the objects")
    print(tenants[0].get_json())&lt;/PRE&gt;&lt;P&gt;This gives us the JSON payload you would want to use to push updates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;lets use the get_json method on one of the objects:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;{'fvTenant': {'attributes': {'name': 'infra'}, 'children': []}}&lt;/PRE&gt;&lt;P&gt;To iterate over the tenants in the list and print their name you can use the .name method:&lt;/P&gt;&lt;PRE&gt;    for tenant in tenants:
        print(tenant.name)&lt;/PRE&gt;&lt;P&gt;My suggestion is to use the API Inspector to develop your payload templates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Apr 2020 14:21:55 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/json-format-from-cobra-aci-toolkit-query/m-p/4065493#M98</guid>
      <dc:creator>Claudia de Luna</dc:creator>
      <dc:date>2020-04-14T14:21:55Z</dc:date>
    </item>
    <item>
      <title>Re: Json format from Cobra/ACI-toolkit query</title>
      <link>https://community.cisco.com/t5/controllers/json-format-from-cobra-aci-toolkit-query/m-p/4067620#M99</link>
      <description>Thanks Derek, the mo iteration was the bit I was missing. Nice one.&lt;BR /&gt;&lt;BR /&gt;Cheers!</description>
      <pubDate>Thu, 16 Apr 2020 21:57:49 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/json-format-from-cobra-aci-toolkit-query/m-p/4067620#M99</guid>
      <dc:creator>m.papini</dc:creator>
      <dc:date>2020-04-16T21:57:49Z</dc:date>
    </item>
    <item>
      <title>Re: Json format from Cobra/ACI-toolkit query</title>
      <link>https://community.cisco.com/t5/controllers/json-format-from-cobra-aci-toolkit-query/m-p/4067622#M100</link>
      <description>&lt;P&gt;Thanks Claudia, I did run into that output using the Toolkit and with your input now I'm able to dissect that entirely, very helpful.&lt;/P&gt;&lt;P&gt;Thanks a million.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Massimo&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 22:02:13 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/json-format-from-cobra-aci-toolkit-query/m-p/4067622#M100</guid>
      <dc:creator>m.papini</dc:creator>
      <dc:date>2020-04-16T22:02:13Z</dc:date>
    </item>
  </channel>
</rss>

