<?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: Joining two keys with the same subkey value in Cortex XSOAR Discussions</title>
    <link>https://live.paloaltonetworks.com/t5/cortex-xsoar-discussions/joining-two-keys-with-the-same-subkey-value/m-p/488558#M853</link>
    <description>&lt;P&gt;Hi &lt;a href="https://live.paloaltonetworks.com/t5/user/viewprofilepage/user-id/208028"&gt;@jfernandes1&lt;/a&gt;&amp;nbsp;, thanks, your POC code actually works! I would like to make a generic case from this. You specifically use&lt;/P&gt;&lt;LI-CODE lang="python"&gt;item['Hostname'] = x['Hostname'] &lt;/LI-CODE&gt;&lt;P&gt;How can I get and use this field, if it is different next time; or there are more than 1&amp;nbsp; subkeys under &lt;STRONG&gt;Hostnames&lt;/STRONG&gt;? So far I came with below code, but not sure how to handle &lt;STRONG&gt;Hostname.&lt;/STRONG&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;args = demisto.args()
parent = args.get('parent')
key1 = args.get('key1')
key2 = args.get('key2')
index1 = args.get('index1')
index2 = args.get('index2')

data = demisto.context()[parent]
newList = []
for item in data[key1]:
    for x in data[key2]:
        if x[index2] == item[index1]:
            item['Hostname'] = x['Hostname']
            newList.append(item)


results = CommandResults(
    outputs_prefix='Output',
    outputs_key_field='Result',
    outputs=newList
)

return_results(results)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 19 May 2022 11:42:23 GMT</pubDate>
    <dc:creator>Antanas</dc:creator>
    <dc:date>2022-05-19T11:42:23Z</dc:date>
    <item>
      <title>Joining two keys with the same subkey value</title>
      <link>https://live.paloaltonetworks.com/t5/cortex-xsoar-discussions/joining-two-keys-with-the-same-subkey-value/m-p/487258#M839</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to join 2 context keys that match value of the same subkey, however I can't find a proper automation or transformation. In context I have 2 keys. One key contains IP's and Hostnames, the other contains IP's and Email Admins. I want to join those that match same IP. E.g. :&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="unnamed (1).png" style="width: 304px;"&gt;&lt;img src="https://live.paloaltonetworks.com/t5/image/serverpage/image-id/40996iA59B5E6BCB4FFB51/image-size/large/is-moderation-mode/true?v=v2&amp;amp;px=999" role="button" title="unnamed (1).png" alt="unnamed (1).png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the above scenario, as a result I want to have a key, that would hold: &lt;STRONG&gt;IP,Hostname,AdminEmail&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;Antanas&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 May 2022 07:40:22 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/cortex-xsoar-discussions/joining-two-keys-with-the-same-subkey-value/m-p/487258#M839</guid>
      <dc:creator>Antanas</dc:creator>
      <dc:date>2022-05-15T07:40:22Z</dc:date>
    </item>
    <item>
      <title>Re: Joining two keys with the same subkey value</title>
      <link>https://live.paloaltonetworks.com/t5/cortex-xsoar-discussions/joining-two-keys-with-the-same-subkey-value/m-p/487294#M840</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://live.paloaltonetworks.com/t5/user/viewprofilepage/user-id/218893"&gt;@Antanas&lt;/a&gt;, not sure if there is a system transformer that can do a complex operation like that. You'll need to write a custom automation. Maybe something like this...&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;data = demisto.context()['Data']
newList = []
for item in data['Emails']:
    for x in data['Hostnames']:
        if x['IP'] == item['IP']:
            item['Hostname'] = x['Hostname'] 
            newList.append(item)
demisto.results(newList)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 May 2022 22:22:57 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/cortex-xsoar-discussions/joining-two-keys-with-the-same-subkey-value/m-p/487294#M840</guid>
      <dc:creator>jfernandes1</dc:creator>
      <dc:date>2022-05-15T22:22:57Z</dc:date>
    </item>
    <item>
      <title>Re: Joining two keys with the same subkey value</title>
      <link>https://live.paloaltonetworks.com/t5/cortex-xsoar-discussions/joining-two-keys-with-the-same-subkey-value/m-p/488558#M853</link>
      <description>&lt;P&gt;Hi &lt;a href="https://live.paloaltonetworks.com/t5/user/viewprofilepage/user-id/208028"&gt;@jfernandes1&lt;/a&gt;&amp;nbsp;, thanks, your POC code actually works! I would like to make a generic case from this. You specifically use&lt;/P&gt;&lt;LI-CODE lang="python"&gt;item['Hostname'] = x['Hostname'] &lt;/LI-CODE&gt;&lt;P&gt;How can I get and use this field, if it is different next time; or there are more than 1&amp;nbsp; subkeys under &lt;STRONG&gt;Hostnames&lt;/STRONG&gt;? So far I came with below code, but not sure how to handle &lt;STRONG&gt;Hostname.&lt;/STRONG&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;args = demisto.args()
parent = args.get('parent')
key1 = args.get('key1')
key2 = args.get('key2')
index1 = args.get('index1')
index2 = args.get('index2')

data = demisto.context()[parent]
newList = []
for item in data[key1]:
    for x in data[key2]:
        if x[index2] == item[index1]:
            item['Hostname'] = x['Hostname']
            newList.append(item)


results = CommandResults(
    outputs_prefix='Output',
    outputs_key_field='Result',
    outputs=newList
)

return_results(results)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 May 2022 11:42:23 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/cortex-xsoar-discussions/joining-two-keys-with-the-same-subkey-value/m-p/488558#M853</guid>
      <dc:creator>Antanas</dc:creator>
      <dc:date>2022-05-19T11:42:23Z</dc:date>
    </item>
    <item>
      <title>Re: Joining two keys with the same subkey value</title>
      <link>https://live.paloaltonetworks.com/t5/cortex-xsoar-discussions/joining-two-keys-with-the-same-subkey-value/m-p/488724#M861</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://live.paloaltonetworks.com/t5/user/viewprofilepage/user-id/218893"&gt;@Antanas&lt;/a&gt;, could you try this. Save the text as yml file and upload it to the automations page. Its more flexible. you can issue the command like below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;!joinListWithLookup list1=${data.data.Emails} list2=${data.data.Hostnames} matchingKey=IP&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;commonfields:
  id: cfc605f7-6d30-4ea0-8330-dacb34e60397
  version: 7
vcShouldKeepItemLegacyProdMachine: false
name: joinListWithLookup
script: |-
  # Accept the input as arguments
  list1 = demisto.args()['list1']
  list2 = demisto.args()['list2']

  # Key that should be tested for matches. Since the order is unknown
  keyMatch = demisto.args()['matchingKey']

  # Validate the input
  error = 1
  if isinstance(list1, list):
      if isinstance(list2, list):
          if isinstance(keyMatch, str):
              error = 0
  if error == 1:
      return_error("Invalid inputs")

  newList = []
  # Loop primary list
  for item in list1:
      # Loop secondary list
      for x in list2:
          # Check if there is a matching key between the lists
          if x[keyMatch] == item[keyMatch]:
              # Add new keys from secondary list to primary
              for key in x.keys():
                  # Only add if they key does not exist, prevent existing data from being overwritten
                  if key not in item.keys():
                      item[key] = x[key]
              # Join data to new list
              newList.append(item)
  # In the last example I included a simple output.
  # The below example add data to the context and the warroom (A nice table)
  demisto.results({
      'Type' : entryTypes['note'],
      'Contents': newList,
      'ContentsFormat' : formats['json'],
      'HumanReadable': tableToMarkdown('New Table', newList),
      'ReadableContentsFormat' : formats['markdown'],
      'EntryContext' : {'newList' : newList }
  })
type: python
tags: []
enabled: true
args:
- name: list1
  required: true
  isArray: true
- name: list2
  required: true
  isArray: true
- name: matchingKey
  required: true
scripttarget: 0
subtype: python3
pswd: ""
runonce: false
dockerimage: demisto/python3:3.10.4.29342
runas: DBotWeakRole
engineinfo: {}
mainengineinfo: {}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 May 2022 02:14:23 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/cortex-xsoar-discussions/joining-two-keys-with-the-same-subkey-value/m-p/488724#M861</guid>
      <dc:creator>jfernandes1</dc:creator>
      <dc:date>2022-05-20T02:14:23Z</dc:date>
    </item>
    <item>
      <title>Re: Joining two keys with the same subkey value</title>
      <link>https://live.paloaltonetworks.com/t5/cortex-xsoar-discussions/joining-two-keys-with-the-same-subkey-value/m-p/489342#M863</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://live.paloaltonetworks.com/t5/user/viewprofilepage/user-id/208028"&gt;@jfernandes1&lt;/a&gt;&amp;nbsp;, this worked like charm! I tested this with a bigger set of real life data, all appears as expected. For the record, I made some changes to be even more generic: added matchingKey2 in case the key name is not the same in both lists; added a "parent" key so I could define each time where the results would be stored in. The final code is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;commonfields:
  id: cfc605f7-6d30-4ea0-8330-dacb34e60397
  version: 10
vcShouldKeepItemLegacyProdMachine: false
name: joinListWithLookup
script: |
  # Accept the input as arguments
  list1 = demisto.args()['list1']
  list2 = demisto.args()['list2']

  # Key that should be tested for matches. Since the order is unknown
  keyMatch1 = demisto.args()['matchingKey1']
  keyMatch2 = demisto.args()['matchingKey2']
  parent = demisto.args()['parent']

  # Validate the input
  error = 1
  if isinstance(list1, list):
      if isinstance(list2, list):
          if isinstance(keyMatch1, str):
              if isinstance(keyMatch2, str):
                  error = 0
  if error == 1:
      return_error("Invalid inputs")

  newList = []
  # Loop primary list
  for item in list1:
      # Loop secondary list
      for x in list2:
          # Check if there is a matching key between the lists
          if x[keyMatch2] == item[keyMatch1]:
              # Add new keys from secondary list to primary
              for key in x.keys():
                  # Only add if they key does not exist, prevent existing data from being overwritten
                  if key not in item.keys():
                      item[key] = x[key]
              # Join data to new list
              newList.append(item)
  # In the last example I included a simple output.
  # The below example add data to the context and the warroom (A nice table)
  demisto.results({
      'Type' : entryTypes['note'],
      'Contents': newList,
      'ContentsFormat' : formats['json'],
      'HumanReadable': tableToMarkdown('New Table', newList),
      'ReadableContentsFormat' : formats['markdown'],
      'EntryContext' : {parent : newList }
  })
type: python
tags: []
comment: joins to context entries, where matchingKey1=matchingKey2, e.g. !joinListWithLookup
  list1=${data.list1} list2=${list2} matchingKey1=key1 matchingKey2=key2 parent=results
enabled: true
args:
- name: list1
  required: true
  description: first list, e.g. ${list1}
  isArray: true
- name: list2
  required: true
  description: second list, e.g. ${list2}
  isArray: true
- name: matchingKey1
  required: true
  description: key from 1st list to match
- name: matchingKey2
  required: true
  description: key from 2nd list to match
- name: parent
  required: true
  description: new key to put joined results in
scripttarget: 0
subtype: python3
pswd: ""
runonce: false
dockerimage: demisto/python3:3.10.4.29342
runas: DBotWeakRole
engineinfo: {}
mainengineinfo: {}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 May 2022 11:22:37 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/cortex-xsoar-discussions/joining-two-keys-with-the-same-subkey-value/m-p/489342#M863</guid>
      <dc:creator>Antanas</dc:creator>
      <dc:date>2022-05-22T11:22:37Z</dc:date>
    </item>
  </channel>
</rss>

