<?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: extract key value pairs and timestamp field from nested json in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334729#M61895</link>
    <description>&lt;P&gt;The timestamp is working fine but uploading from python code the columns are not getting splitted as it is while we upload json file from UI.&lt;BR /&gt;
Image snapshot for data uploaded directly from splunk UI (Add Data).&lt;BR /&gt;
&lt;A href="https://prnt.sc/hoy25e"&gt;https://prnt.sc/hoy25e&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 18 Dec 2017 07:17:35 GMT</pubDate>
    <dc:creator>sawgata12345</dc:creator>
    <dc:date>2017-12-18T07:17:35Z</dc:date>
    <item>
      <title>extract key value pairs and timestamp field from nested json</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334720#M61886</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I need to read the below json file in python script and send each json to splunk.&lt;BR /&gt;
[[[&lt;BR /&gt;
     with open('sampledata.json') as jsond:&lt;BR /&gt;
         jsondata = json.load(jsond)&lt;BR /&gt;
     for line in jsondata:&lt;BR /&gt;
      target.submit(event=str(line),sourcetype=sourcetype)&lt;BR /&gt;
          time.sleep(5)&lt;BR /&gt;
]]]&lt;/P&gt;

&lt;P&gt;How to split each key value pair in json to "search fields" in splunk and also extract the "timestamp" field from json to be used by splunk as event timestamp?&lt;/P&gt;

&lt;P&gt;[&lt;BR /&gt;
  {&lt;BR /&gt;
    "id": 707860,&lt;BR /&gt;
    "name": "Hurzuf",&lt;BR /&gt;
    "country": "UA",&lt;BR /&gt;
    "timestamp":"2017-09-02 06:44:14",&lt;BR /&gt;
    "coord": {&lt;BR /&gt;
      "lon": 34.283333,&lt;BR /&gt;
      "lat": 44.549999&lt;BR /&gt;
    },&lt;BR /&gt;
    "ports":[&lt;BR /&gt;
    {&lt;BR /&gt;
      "port": 1,&lt;BR /&gt;
      "utilization": 140,&lt;BR /&gt;
      "error": {&lt;BR /&gt;
         "tx": 1000.00,&lt;BR /&gt;
         "rx": 500&lt;BR /&gt;
      }&lt;BR /&gt;
    },&lt;BR /&gt;
    {&lt;BR /&gt;
      "port": 2,&lt;BR /&gt;
      "utilization": 110,&lt;BR /&gt;
][1]      "error": {&lt;BR /&gt;
         "tx": 1002.00,&lt;BR /&gt;
         "rx": 420&lt;BR /&gt;
      }&lt;BR /&gt;
    }&lt;BR /&gt;
    ]&lt;BR /&gt;
  },&lt;BR /&gt;
  {&lt;BR /&gt;
    "id": 519188,&lt;BR /&gt;
    "name": "Novinki",&lt;BR /&gt;
    "country": "RU",&lt;BR /&gt;
    "timestamp":"2017-09-03 06:50:14",&lt;BR /&gt;
    "coord": {&lt;BR /&gt;
      "lon": 37.666668,&lt;BR /&gt;
      "lat": 55.683334&lt;BR /&gt;
    },&lt;BR /&gt;
    "ports":[&lt;BR /&gt;
    {&lt;BR /&gt;
      "port": 1,&lt;BR /&gt;
      "utilization": 120,&lt;BR /&gt;
      "error": {&lt;BR /&gt;
         "tx": 1020.00,&lt;BR /&gt;
         "rx": 400&lt;BR /&gt;
      }&lt;BR /&gt;
    },&lt;BR /&gt;
    {&lt;BR /&gt;
      "port": 2,&lt;BR /&gt;
      "utilization": 120,&lt;BR /&gt;
      "error": {&lt;BR /&gt;
         "tx": 1002.00,&lt;BR /&gt;
         "rx": 400&lt;BR /&gt;
      }&lt;BR /&gt;
    }&lt;BR /&gt;
    ]&lt;BR /&gt;
  }&lt;BR /&gt;
]&lt;/P&gt;

&lt;P&gt;If I directly upload the json file the screen looks like the image attached.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2017 11:22:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334720#M61886</guid>
      <dc:creator>sawgata12345</dc:creator>
      <dc:date>2017-12-12T11:22:49Z</dc:date>
    </item>
    <item>
      <title>Re: extract key value pairs and timestamp field from nested json</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334721#M61887</link>
      <description>&lt;P&gt;Try selecting json related sourcetypes. There are couple of sourcetype&lt;/P&gt;

&lt;P&gt;Also Try adding &lt;BR /&gt;
    TIME_PREFIX = \"timestamp\":\"&lt;/P&gt;

&lt;P&gt;In the screenshot&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2017 12:24:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334721#M61887</guid>
      <dc:creator>mayurr98</dc:creator>
      <dc:date>2017-12-12T12:24:29Z</dc:date>
    </item>
    <item>
      <title>Re: extract key value pairs and timestamp field from nested json</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334722#M61888</link>
      <description>&lt;P&gt;Hi @sawgata12345,&lt;/P&gt;

&lt;P&gt;Based on sample data I have uploaded below sample data in splunk with &lt;CODE&gt;_json&lt;/CODE&gt; sourcetype and it is working fine and extracting fields properly.&lt;/P&gt;

&lt;P&gt;Sample data:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[ 
{ 
"id": 707860, 
"name": "Hurzuf", 
"country": "UA", 
"timestamp":"2017-09-02 06:44:14", 
"coord": { "lon": 34.283333, "lat": 44.549999 }, 
"ports": [ { "port": 1, "utilization": 140, "error": { "tx": 1000.00, "rx": 500 } }, { "port": 2, "utilization": 110, "error": { "tx": 1002.00, "rx": 420 } } ]
},
{
"id": 519188,
"name": "Novinki",
"country": "RU",
"timestamp":"2017-09-03 06:50:14",
"coord": { "lon": 37.666668, "lat": 55.683334 },
"ports":[ { "port": 1, "utilization": 120, "error": {"tx": 1020.00, "rx": 400 } },
{ "port": 2, "utilization": 120, "error": { "tx": 1002.00, "rx": 400 } } ]
}
]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Screenshot:&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/3985iF8B1BB3CCA41372E/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2017 12:27:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334722#M61888</guid>
      <dc:creator>harsmarvania57</dc:creator>
      <dc:date>2017-12-12T12:27:10Z</dc:date>
    </item>
    <item>
      <title>Re: extract key value pairs and timestamp field from nested json</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334723#M61889</link>
      <description>&lt;P&gt;@mayurr98&lt;/P&gt;

&lt;P&gt;I have already tried TIME_PREFIX = \"timestamp\":\" also. it didn't work.&lt;BR /&gt;
As above in my query the major issue is when I am sending the data from python to splunk using splunksdk.&lt;BR /&gt;
[[[&lt;BR /&gt;
with open('sampledata.json') as jsond:&lt;BR /&gt;
jsondata = json.load(jsond)&lt;BR /&gt;
for line in jsondata:&lt;BR /&gt;
target.submit(event=str(line),sourcetype=sourcetype)&lt;BR /&gt;
time.sleep(5)&lt;BR /&gt;
]]]&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2017 12:35:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334723#M61889</guid>
      <dc:creator>sawgata12345</dc:creator>
      <dc:date>2017-12-12T12:35:34Z</dc:date>
    </item>
    <item>
      <title>Re: extract key value pairs and timestamp field from nested json</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334724#M61890</link>
      <description>&lt;P&gt;@harsmarvania57&lt;BR /&gt;
see the ports{}:error,rx&lt;BR /&gt;
it should come as port1:error:rx | port1:error:tx|port2:error:rx|port2:error:tx  etc &lt;/P&gt;

&lt;P&gt;and even this much also not coming when i upload the same data from python code as in my query above.&lt;/P&gt;

&lt;P&gt;If sending from python code whole line is coming as event with current time as timestamp&lt;BR /&gt;
timestamp  | event&lt;BR /&gt;
XXXXXX        | { "id": 707860,  "name": "Hurzuf",  "country": "UA",  "timestamp":"2017-09-02 06:44:14",&lt;/P&gt;

&lt;H2&gt; "coord": { "lon": 34.283333, "lat": 44.549999 },  "ports": [ { "port": 1, "utilization": 140, "error": { "tx": 1000.00, "rx": 500 } }, { "port": 2, "utilization": 110, "error": { "tx": 1002.00, "rx": 420 } } ] }&lt;/H2&gt;

&lt;P&gt;XXXXXX        | { "id": 707860,  "name": "Hurzuf",  "country": "UA",  "timestamp":"2017-09-02 06:44:14",&lt;BR /&gt;
 "coord": { "lon": 34.283333, "lat": 44.549999 },  "ports": [ { "port": 1, "utilization": 140, "error": { "tx": 1000.00, "rx": 500 } }, { "port": 2, "utilization": 110, "error": { "tx": 1002.00, "rx": 420 } } ] }&lt;/P&gt;

&lt;P&gt;It dosen't even seperate the Id,name,country which are not nested as "ports".&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2017 12:44:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334724#M61890</guid>
      <dc:creator>sawgata12345</dc:creator>
      <dc:date>2017-12-12T12:44:14Z</dc:date>
    </item>
    <item>
      <title>Re: extract key value pairs and timestamp field from nested json</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334725#M61891</link>
      <description>&lt;P&gt;When you are sending data from python which source type are you supplying?&lt;/P&gt;

&lt;P&gt;Have you tried with &lt;CODE&gt;_json&lt;/CODE&gt; sourcetype?&lt;/P&gt;

&lt;P&gt;And ports{}.error.rx is coming because ports has nested json value so it will come as ports{}.error.rx not port:error:rx&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2017 16:01:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334725#M61891</guid>
      <dc:creator>harsmarvania57</dc:creator>
      <dc:date>2017-12-12T16:01:42Z</dc:date>
    </item>
    <item>
      <title>Re: extract key value pairs and timestamp field from nested json</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334726#M61892</link>
      <description>&lt;P&gt;I have created python script called test.py and I have already installed splunk-python-sdk&lt;/P&gt;

&lt;P&gt;test.py script &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import json
import splunklib.client as client

HOST = "localhost"
PORT = 8089
USERNAME = "admin"
PASSWORD = "changeme"

# Create a Service instance and log in 
service = client.connect(
    host=HOST,
    port=PORT,
    username=USERNAME,
    password=PASSWORD)

with open('sampledata.json') as jsond:
    data=json.load(jsond)
for line in data:
    target=service.indexes["main"]
    data_json=json.dumps(line)
    target.submit(event=data_json,sourcetype="json")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Sample data in sampledata.json file&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[ 
 { 
 "id": 707860, 
 "name": "Hurzuf", 
 "country": "UA", 
 "timestamp":"2017-09-02 06:44:14", 
 "coord": { "lon": 34.283333, "lat": 44.549999 }, 
 "ports": [ { "port": 1, "utilization": 140, "error": { "tx": 1000.00, "rx": 500 } }, { "port": 2, "utilization": 110, "error": { "tx": 1002.00, "rx": 420 } } ]
 },
 {
 "id": 519188,
 "name": "Novinki",
 "country": "RU",
 "timestamp":"2017-09-03 06:50:14",
 "coord": { "lon": 37.666668, "lat": 55.683334 },
 "ports":[ { "port": 1, "utilization": 120, "error": {"tx": 1020.00, "rx": 400 } },
 { "port": 2, "utilization": 120, "error": { "tx": 1002.00, "rx": 400 } } ]
 }
 ]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Which is giving me perfect result, please find screenshot here &lt;A href="https://prnt.sc/hmu3s6"&gt;https://prnt.sc/hmu3s6&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;If you want to rename field names from &lt;CODE&gt;ports{}&lt;/CODE&gt; to &lt;CODE&gt;ports:&lt;/CODE&gt; you can use &lt;CODE&gt;rename&lt;/CODE&gt; command like this &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;yourBasesearch&amp;gt; | rename ports{}.error.rx AS ports:error:rx, ports{}.error.tx AS ports:error:tx, ports{}.port AS ports:port, ports{}.utilization AS ports:utilization
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I hope this helps.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 08:28:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334726#M61892</guid>
      <dc:creator>harsmarvania57</dc:creator>
      <dc:date>2017-12-13T08:28:54Z</dc:date>
    </item>
    <item>
      <title>Re: extract key value pairs and timestamp field from nested json</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334727#M61893</link>
      <description>&lt;P&gt;Thats the issue, the whole json is coming in single block and the timestamp is also not extracted. &lt;/P&gt;

&lt;P&gt;In your upload the event time is showing as the time of upload for all json and not that which is mentioned inside each json.&lt;BR /&gt;
And see(the image which I had uploaded with the Question) how the fields get seperated if we upload whole json file manually using System-&amp;gt;AppData-&amp;gt;upload.&lt;BR /&gt;
  _time                                                                              | coord:lat | cooord:long|country | Id |name ........&lt;BR /&gt;
(timeshould be from the "timestamp" field in json)&lt;/P&gt;

&lt;P&gt;This extraction is not happening form python upload, and the events are not getting segregated using the "timestamp" field inside each json.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 12:20:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334727#M61893</guid>
      <dc:creator>sawgata12345</dc:creator>
      <dc:date>2017-12-13T12:20:00Z</dc:date>
    </item>
    <item>
      <title>Re: extract key value pairs and timestamp field from nested json</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334728#M61894</link>
      <description>&lt;P&gt;You have 2 json blocks in your sample data so it will generate 2 events in splunk, still I am not clear what you are trying to say that "it is coming in single block".&lt;/P&gt;

&lt;P&gt;Based on comment which I have provided earlier in which I have changed sourcetype in test.py &lt;BR /&gt;
script to &lt;CODE&gt;target.submit(event=data_json,sourcetype="custom_json")&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;And I have created &lt;CODE&gt;$SPLUNK_HOME/etc/system/local/props.conf&lt;/CODE&gt; with below content for &lt;CODE&gt;custom_json&lt;/CODE&gt; sourcetype&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[custom_json]
KV_MODE = json
TIME_PREFIX = timestamp
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Which is extracting timestamp correctly, please refer screenshot &lt;A href="https://prnt.sc/hn0h1l"&gt;https://prnt.sc/hn0h1l&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 15:56:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334728#M61894</guid>
      <dc:creator>harsmarvania57</dc:creator>
      <dc:date>2017-12-13T15:56:05Z</dc:date>
    </item>
    <item>
      <title>Re: extract key value pairs and timestamp field from nested json</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334729#M61895</link>
      <description>&lt;P&gt;The timestamp is working fine but uploading from python code the columns are not getting splitted as it is while we upload json file from UI.&lt;BR /&gt;
Image snapshot for data uploaded directly from splunk UI (Add Data).&lt;BR /&gt;
&lt;A href="https://prnt.sc/hoy25e"&gt;https://prnt.sc/hoy25e&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2017 07:17:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334729#M61895</guid>
      <dc:creator>sawgata12345</dc:creator>
      <dc:date>2017-12-18T07:17:35Z</dc:date>
    </item>
    <item>
      <title>Re: extract key value pairs and timestamp field from nested json</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334730#M61896</link>
      <description>&lt;P&gt;When you index anything in Splunk it will display as events only (Refer screenshot which I have provided earlier), if you want to display those data in table format in that case you need to use&lt;CODE&gt;table&lt;/CODE&gt; Command as I have provided earlier. &lt;/P&gt;

&lt;P&gt;Screenshot which you have provided is displaying data when you assigning sourcetype but after that when you index it, it will display as events only (Same as screenshot which I have provided earlier)&lt;/P&gt;

&lt;P&gt;I hope this helps.&lt;/P&gt;

&lt;P&gt;If this clears your query then please accept my answer and upvote it.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
Harshil&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2017 13:50:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/extract-key-value-pairs-and-timestamp-field-from-nested-json/m-p/334730#M61896</guid>
      <dc:creator>harsmarvania57</dc:creator>
      <dc:date>2017-12-18T13:50:33Z</dc:date>
    </item>
  </channel>
</rss>

