<?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: Create Table from json data in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334843#M61921</link>
    <description>&lt;P&gt;@DalJeanis, I was waiting for your answer &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;
@chris please try out this answer and confirm.&lt;/P&gt;</description>
    <pubDate>Thu, 14 Sep 2017 14:57:54 GMT</pubDate>
    <dc:creator>niketn</dc:creator>
    <dc:date>2017-09-14T14:57:54Z</dc:date>
    <item>
      <title>Create Table from json data</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334836#M61914</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;

&lt;P&gt;I have a json input that has the following format:&lt;BR /&gt;
{&lt;BR /&gt;
  "body": {&lt;BR /&gt;
    "1": {&lt;BR /&gt;
      "dataId": 1,&lt;BR /&gt;
      "first": "Mihail",&lt;BR /&gt;
      "address": "Street b"&lt;BR /&gt;
    },&lt;BR /&gt;
    "3": {&lt;BR /&gt;
      "dataId": 3,&lt;BR /&gt;
      "first": "Mickey",&lt;BR /&gt;
      "address": "Street f"&lt;BR /&gt;
    },&lt;BR /&gt;
    "5": {&lt;BR /&gt;
      "dataId": 5,&lt;BR /&gt;
      "first": "Adam",&lt;BR /&gt;
      "address": "Street g"&lt;BR /&gt;
    },&lt;BR /&gt;
    "9": {&lt;BR /&gt;
      "dataId": 1,&lt;BR /&gt;
      "first": "John",&lt;BR /&gt;
      "address": "Street b"&lt;BR /&gt;
    }&lt;BR /&gt;
  }&lt;BR /&gt;
}&lt;/P&gt;

&lt;P&gt;I d like to create a Table from that data as follows (sorry for the ugly Table representation):&lt;BR /&gt;
  &lt;CODE&gt;&lt;BR /&gt;
| dataID    | first     | address  |&lt;BR /&gt;
|------------|------------|----------|&lt;BR /&gt;
| 1           | Mihail    | street b |&lt;BR /&gt;
| 3         | Mickey   | street f |&lt;BR /&gt;
&lt;/CODE&gt;&lt;BR /&gt;
The field names are variable because of the json structure and contain the changing id&lt;/P&gt;

&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/3492i51184C73C8DF5A58/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;Is there a way to make multivalue fields and use mvexpand somehow.&lt;/P&gt;

&lt;P&gt;Regards &lt;BR /&gt;
Chris&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2017 05:31:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334836#M61914</guid>
      <dc:creator>chris</dc:creator>
      <dc:date>2017-09-14T05:31:01Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table from json data</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334837#M61915</link>
      <description>&lt;P&gt;@chris can you explain what you mean by making multivalue fields. If you can add an example of the output you need that would be quite helpful for us to assist.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2017 05:45:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334837#M61915</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-09-14T05:45:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table from json data</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334838#M61916</link>
      <description>&lt;P&gt;Hi, i just want to get a table with the following headers: dataId,first,address (I did not manage to render a proper table using markdown or html). I thought that I will probably have multivalu fields at some point either containing dataId,first and address or one for each column of the table. I do not know how to parse the json so i do not end up having individual fields extracted. If there is a way to avoid mv fields I'm happy with that solution.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2017 07:24:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334838#M61916</guid>
      <dc:creator>chris</dc:creator>
      <dc:date>2017-09-14T07:24:20Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table from json data</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334839#M61917</link>
      <description>&lt;P&gt;have you tried using &lt;CODE&gt;|spath&lt;/CODE&gt; to format this data?&lt;BR /&gt;
&lt;A href="http://docs.splunk.com/Documentation/SplunkCloud/6.6.1/SearchReference/Spath"&gt;http://docs.splunk.com/Documentation/SplunkCloud/6.6.1/SearchReference/Spath&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2017 11:50:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334839#M61917</guid>
      <dc:creator>cmerriman</dc:creator>
      <dc:date>2017-09-14T11:50:34Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table from json data</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334840#M61918</link>
      <description>&lt;P&gt;Try the following run anywhere search&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
| eval _raw="{
\"body\": {
\"1\": {
\"dataId\": 1,
\"first\": \"Mihail\",
\"address\": \"Street b\"
},
\"3\": {
\"dataId\": 3,
\"first\": \"Mickey\",
\"address\": \"Street f\"
},
\"5\": {
\"dataId\": 5,
\"first\": \"Adam\",
\"address\": \"Street g\"
},
\"9\": {
\"dataId\": 1,
\"first\": \"John\",
\"address\": \"Street b\"
}
}
}"
| spath
| table body*
| transpose column_name="field"
| rename "row 1" as "value"
| eval field=split(field,".")
| eval counter=mvindex(field,1)
| eval field=mvindex(field,2)
| table counter value
| stats list(value) as value by counter
| fields - counter
| eval address=mvindex(value,0)
| eval dataId=mvindex(value,1)
| eval first=mvindex(value,2)
| fields - value
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Based on the screenshot, you already have query till  &lt;CODE&gt;| table body*&lt;/CODE&gt;, so try commands after that starting with &lt;CODE&gt;transpose&lt;/CODE&gt;. Try this out and confirm. Also wait for others to answer as there might be a better way!&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;Edit by chris: &lt;/P&gt;

&lt;P&gt;This is another possibility:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
| eval _raw="{
\"body\": {
\"1\": {
\"dataId\": 1,
\"first\": \"Mihail\",
\"address\": \"Street b\"
},
\"3\": {
\"dataId\": 3,
\"first\": \"Mickey\",
\"address\": \"Street f\"
},
\"5\": {
\"dataId\": 5,
\"first\": \"Adam\",
\"address\": \"Street g\"
},
\"9\": {
\"dataId\": 1,
\"first\": \"John\",
\"address\": \"Street b\"
}
}
}"
| spath
| table body* 
| foreach body.*.* [eval &amp;lt;&amp;lt;MATCHSEG2&amp;gt;&amp;gt;=mvappend(&amp;lt;&amp;lt;MATCHSEG2&amp;gt;&amp;gt;,'&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;')]
| eval line=mvzip(mvzip(first,address,":"),dataId,":") 
| mvexpand line 
|  eval x = split(line,":") 
|  eval first=mvindex(x,0) 
|  eval address=mvindex(x,1) 
|  eval dataId=mvindex(x,2) 
|  table first,address,dataId
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Sep 2017 12:58:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334840#M61918</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-09-14T12:58:46Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table from json data</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334841#M61919</link>
      <description>&lt;P&gt;Thanks a lot, I'll leave the question open for a bit, to see if other solutions pop up. &lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2017 13:49:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334841#M61919</guid>
      <dc:creator>chris</dc:creator>
      <dc:date>2017-09-14T13:49:52Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table from json data</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334842#M61920</link>
      <description>&lt;P&gt;Here's another way.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
 | eval _raw="{
 \"body\": {
 \"1\": {
 \"dataId\": 1,
 \"first\": \"Mihail\",
 \"address\": \"Street b\"
 },
 \"3\": {
 \"dataId\": 3,
 \"first\": \"Mickey\",
 \"address\": \"Street f\"
 },
 \"5\": {
 \"dataId\": 5,
 \"first\": \"Adam\",
 \"address\": \"Street g\"
 },
 \"9\": {
 \"dataId\": 9,
 \"first\": \"John\",
 \"address\": \"Street b\"
 }
 }
 }"
 | spath
 | table body*
 | eval recno=1
 | untable recno name value
 | rex field=name "^body\.(?&amp;lt;temp&amp;gt;\d+)\.(?&amp;lt;fieldname&amp;gt;.*)"
 | xyseries temp fieldname value
 | table dataId first address
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Sep 2017 14:12:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334842#M61920</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-09-14T14:12:26Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table from json data</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334843#M61921</link>
      <description>&lt;P&gt;@DalJeanis, I was waiting for your answer &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;
@chris please try out this answer and confirm.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2017 14:57:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334843#M61921</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-09-14T14:57:54Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table from json data</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334844#M61922</link>
      <description>&lt;P&gt;Great this is the best solution so far. It think if you replace the last table command with "| fields - temp" the names of the final columns are not needed in the search language and the search is more flexibel if any of the columns change in the json. Then of course if only a subset of the columns is required table will help.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2017 15:09:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334844#M61922</guid>
      <dc:creator>chris</dc:creator>
      <dc:date>2017-09-14T15:09:34Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table from json data</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334845#M61923</link>
      <description>&lt;P&gt;@niketnilay - heh.  Mine was different enough from your two that I thought it worth posting.&lt;BR /&gt;&lt;BR /&gt;
@chris - glad you like it.  I wasn't able to quickly come up with a way that would work for multiple JSON files at one time that had .... oh, there it is...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;  | table body*
  | streamstats count as recno
  | untable recno name value
  | rex field=name "^body\.(?&amp;lt;temp&amp;gt;\d+)\.(?&amp;lt;fieldname&amp;gt;.*)"
  | eval recno=recno."!!!!".temp 
  | xyseries recno fieldname value
  | eval recno=mvindex(split(recno,"!!!!"),0) 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Something like that would work.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2017 15:21:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334845#M61923</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-09-14T15:21:32Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table from json data</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334846#M61924</link>
      <description>&lt;P&gt;Here's another way to use the &lt;CODE&gt;foreach&lt;/CODE&gt;, that doesn't hard code the field names ... &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; | spath
 | foreach body.*.* [eval data&amp;lt;&amp;lt;MATCHSEG1&amp;gt;&amp;gt;=coalesce(data&amp;lt;&amp;lt;MATCHSEG1&amp;gt;&amp;gt;,"")."!!!!&amp;lt;&amp;lt;MATCHSEG2&amp;gt;&amp;gt;=\"".'&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;'."\""]
 | foreach data* [eval alldata=mvappend(alldata,substr(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,5,len(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;)-4))]
 | table alldata 
 | streamstats count as recno
 | mvexpand alldata
 | streamstats count as recno2 
 | makemv delim="!!!!" alldata
 | mvexpand alldata
 | rex field=alldata "^(?&amp;lt;fieldname&amp;gt;[^=]+)=(?&amp;lt;fieldvalue&amp;gt;.+)"
 | eval {fieldname}=fieldvalue
 | fields - alldata fieldname fieldvalue
 | stats values(*) as * by recno recno2
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Sep 2017 16:43:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Create-Table-from-json-data/m-p/334846#M61924</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-09-14T16:43:41Z</dc:date>
    </item>
  </channel>
</rss>

