<?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: Merging data from JSON snippets in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Merging-data-from-JSON-snippets/m-p/740934#M240501</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/255974"&gt;@rikinet&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would the following achieve what you're looking for?&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="livehybrid_0-1741187010912.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/38019i5A151048A4487C8A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="livehybrid_0-1741187010912.png" alt="livehybrid_0-1741187010912.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults count=5 
| streamstats count as a 
| eval _time = _time + (60*a) 
| eval
    json1="{\"id\":1,\"attrib_A\":\"A1\"}#{\"id\":2,\"attrib_A\":\"A2\"}#{\"id\":3,\"attrib_A\":\"A3\"}#{\"id\":4,\"attrib_A\":\"A4\"}#{\"id\":5,\"attrib_A\":\"A5\"}",
    json2="{\"id\":2,\"attrib_B\":\"B2\"}#{\"id\":3,\"attrib_B\":\"B3\"}#{\"id\":4,\"attrib_B\":\"B4\"}#{\"id\":6,\"attrib_B\":\"B6\"}" 
| makemv delim="#" json1 
| makemv delim="#" json2
``` end data prep ```
| eval data=mvappend(json1,json2)
| mvexpand data
| spath input=data path=id output=id
| spath input=data path=attrib_A output=attrib_A
| spath input=data path=attrib_B output=attrib_B
| stats values(attrib_A) as attrib_A values(attrib_B) as attrib_B by id
| table id, attrib_A, attrib_B&lt;/LI-CODE&gt;&lt;P&gt;Please let me know how you get on and consider adding karma to this or any other answer if it has helped.&lt;BR /&gt;Regards&lt;/P&gt;&lt;P&gt;Will&lt;/P&gt;</description>
    <pubDate>Wed, 05 Mar 2025 15:03:54 GMT</pubDate>
    <dc:creator>livehybrid</dc:creator>
    <dc:date>2025-03-05T15:03:54Z</dc:date>
    <item>
      <title>Merging data from JSON snippets</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Merging-data-from-JSON-snippets/m-p/740917#M240498</link>
      <description>&lt;P&gt;Dear Splunk community,&lt;/P&gt;&lt;P&gt;I have following sample input data, containing JSON snippets in MV fields:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults count=5 | streamstats count as a | eval _time = _time + (60*a)
| eval
  json1="{\"id\":1,\"attrib_A\":\"A1\"}#{\"id\":2,\"attrib_A\":\"A2\"}#{\"id\":3,\"attrib_A\":\"A3\"}#{\"id\":4,\"attrib_A\":\"A4\"}#{\"id\":5,\"attrib_A\":\"A5\"}",
  json2="{\"id\":2,\"attrib_B\":\"B2\"}#{\"id\":3,\"attrib_B\":\"B3\"}#{\"id\":4,\"attrib_B\":\"B4\"}#{\"id\":6,\"attrib_B\":\"B6\"}"
| makemv delim="#" json1
| makemv delim="#" json2
| table _time, json1, json2&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The lists of ids in json1 and json2 may be disjoint, identical or overlap. For example, in above data, id=1 and id=5 only exist in json1, id=6 only exists in json2, the other ids exist in both. Attributes can be null values, but may then be treated as if the id didn't exist.&lt;/P&gt;&lt;P&gt;For each event, I would like to merge the data from json1 and json2 into a single table with columns id, attrib_A and attrib_B.&lt;/P&gt;&lt;P&gt;The expected output for the sample data would be:&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;&lt;STRONG&gt;_time&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;&lt;STRONG&gt;id&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;&lt;STRONG&gt;attrib_A&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;&lt;STRONG&gt;attrib_B&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="24px"&gt;t&lt;/TD&gt;&lt;TD width="25%" height="24px"&gt;1&lt;/TD&gt;&lt;TD width="25%" height="24px"&gt;A1&lt;/TD&gt;&lt;TD width="25%" height="24px"&gt;&lt;EM&gt;null&lt;/EM&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;t&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;2&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;A2&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;B2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;t&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;3&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;A3&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;B3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;t&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;4&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;A4&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;B4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;t&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;5&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;A5&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;&lt;EM&gt;null&lt;/EM&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;t&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;6&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;&lt;EM&gt;null&lt;/EM&gt;&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;B6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;...&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;...&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;...&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;...&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;t+5&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;1&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;A1&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;&lt;EM&gt;null&lt;/EM&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;t+5&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;2&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;A2&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;B2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="25px"&gt;t+5&lt;/TD&gt;&lt;TD height="25px"&gt;3&lt;/TD&gt;&lt;TD height="25px"&gt;A3&lt;/TD&gt;&lt;TD height="25px"&gt;B3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="25px"&gt;t+5&lt;/TD&gt;&lt;TD height="25px"&gt;4&lt;/TD&gt;&lt;TD height="25px"&gt;A4&lt;/TD&gt;&lt;TD height="25px"&gt;B4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="25px"&gt;t+5&lt;/TD&gt;&lt;TD height="25px"&gt;5&lt;/TD&gt;&lt;TD height="25px"&gt;A5&lt;/TD&gt;&lt;TD height="25px"&gt;&lt;EM&gt;null&lt;/EM&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="25px"&gt;t+5&lt;/TD&gt;&lt;TD height="25px"&gt;6&lt;/TD&gt;&lt;TD height="25px"&gt;&lt;EM&gt;null&lt;/EM&gt;&lt;/TD&gt;&lt;TD height="25px"&gt;B6&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;How can I achieve this in a straighforward way?&lt;/P&gt;&lt;P&gt;The following works for the sample data, but it seems overly complicated and am not sure if it works in all cases:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;```insert after above sample data generation:```

```extract and expand JSONs```
| mvexpand json2
| spath input=json2
| rename id as json2_id

| mvexpand json1
| spath input=json1
| rename id as json1_id

| table _time, json1_id, attrib_A, json2_id, attrib_B

```create mv fields containing the subsets of IDs from json1 and json2```
| eventstats values(json1_id) as json1, values(json2_id) as json2 by _time
| eval only_json1=mvmap(json1, if(isnull(mvfind(json2, json1)), json1, null()))
| eval only_json2=mvmap(json2, if(isnull(mvfind(json1, json2)), json2, null()))
| eval both=mvmap(json1, if(isnotnull(mvfind(json2, json1)), json1, null()))

| table _time, json1_id, attrib_A, json2_id, attrib_B, json1, json2, only_json1, only_json2, both

```keep json2 record if a) json2_id equals json1_id or b) json2_id does not appear in json1```
| eval attrib_B=if(json2_id==json1_id or isnull(mvfind(json1, json2_id)), attrib_B, null())
| eval json2_id=if(json2_id==json1_id or isnull(mvfind(json1, json2_id)), json2_id, null())

```keep json1 record if a) json1_id equals json2_id or b) json1_id does not appear in json2```
| eval attrib_A=if(json1_id==json2_id or isnull(mvfind(json2, json1_id)), attrib_A, null())
| eval json1_id=if(json1_id==json2_id or isnull(mvfind(json2, json1_id)), json1_id, null())

```remove records where json1 and json2 are both null```
| where isnotnull(json1_id) or isnotnull(json2_id)

| table _time, json1_id, attrib_A, json2_id, attrib_B
| dedup _time, json1_id, attrib_A&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 05 Mar 2025 12:19:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Merging-data-from-JSON-snippets/m-p/740917#M240498</guid>
      <dc:creator>rikinet</dc:creator>
      <dc:date>2025-03-05T12:19:40Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data from JSON snippets</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Merging-data-from-JSON-snippets/m-p/740934#M240501</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/255974"&gt;@rikinet&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would the following achieve what you're looking for?&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="livehybrid_0-1741187010912.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/38019i5A151048A4487C8A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="livehybrid_0-1741187010912.png" alt="livehybrid_0-1741187010912.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults count=5 
| streamstats count as a 
| eval _time = _time + (60*a) 
| eval
    json1="{\"id\":1,\"attrib_A\":\"A1\"}#{\"id\":2,\"attrib_A\":\"A2\"}#{\"id\":3,\"attrib_A\":\"A3\"}#{\"id\":4,\"attrib_A\":\"A4\"}#{\"id\":5,\"attrib_A\":\"A5\"}",
    json2="{\"id\":2,\"attrib_B\":\"B2\"}#{\"id\":3,\"attrib_B\":\"B3\"}#{\"id\":4,\"attrib_B\":\"B4\"}#{\"id\":6,\"attrib_B\":\"B6\"}" 
| makemv delim="#" json1 
| makemv delim="#" json2
``` end data prep ```
| eval data=mvappend(json1,json2)
| mvexpand data
| spath input=data path=id output=id
| spath input=data path=attrib_A output=attrib_A
| spath input=data path=attrib_B output=attrib_B
| stats values(attrib_A) as attrib_A values(attrib_B) as attrib_B by id
| table id, attrib_A, attrib_B&lt;/LI-CODE&gt;&lt;P&gt;Please let me know how you get on and consider adding karma to this or any other answer if it has helped.&lt;BR /&gt;Regards&lt;/P&gt;&lt;P&gt;Will&lt;/P&gt;</description>
      <pubDate>Wed, 05 Mar 2025 15:03:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Merging-data-from-JSON-snippets/m-p/740934#M240501</guid>
      <dc:creator>livehybrid</dc:creator>
      <dc:date>2025-03-05T15:03:54Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data from JSON snippets</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Merging-data-from-JSON-snippets/m-p/740976#M240508</link>
      <description>&lt;P&gt;Either I'm missing something here or you're overly complicating it.&lt;/P&gt;&lt;P&gt;Why there is a separate json1 and json2?&lt;/P&gt;&lt;P&gt;Sticking with your mockup data&lt;/P&gt;&lt;PRE&gt;| makeresults count=5 | streamstats count as a | eval _time = _time + (60*a)&lt;BR /&gt;| eval&lt;BR /&gt;json1="{\"id\":1,\"attrib_A\":\"A1\"}#{\"id\":2,\"attrib_A\":\"A2\"}#{\"id\":3,\"attrib_A\":\"A3\"}#{\"id\":4,\"attrib_A\":\"A4\"}#{\"id\":5,\"attrib_A\":\"A5\"}",&lt;BR /&gt;json2="{\"id\":2,\"attrib_B\":\"B2\"}#{\"id\":3,\"attrib_B\":\"B3\"}#{\"id\":4,\"attrib_B\":\"B4\"}#{\"id\":6,\"attrib_B\":\"B6\"}"&lt;BR /&gt;| makemv delim="#" json1&lt;BR /&gt;| makemv delim="#" json2&lt;BR /&gt;| eval json=mvappend(json1,json2)&lt;BR /&gt;| fields _time json&lt;BR /&gt;| mvexpand json&lt;BR /&gt;| spath input=json&lt;BR /&gt;| fields - json&lt;BR /&gt;| stats values(attrib_*) as * by _time id&lt;/PRE&gt;&lt;P&gt;You can of course add fillnull at the end.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Mar 2025 21:45:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Merging-data-from-JSON-snippets/m-p/740976#M240508</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2025-03-05T21:45:18Z</dc:date>
    </item>
  </channel>
</rss>

