<?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: REST API - JSON Invalid format in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36214#M6607</link>
    <description>&lt;P&gt;If you have 10-15 rows and none of them are previews, then you can simply split on newlines (&lt;CODE&gt;\n&lt;/CODE&gt;), and then parse each string as a JSON object and meld them together. If some of them are previews, basically do the same, just ignore anything that has the &lt;CODE&gt;preview&lt;/CODE&gt; property set to &lt;CODE&gt;true&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;Note that since you have so few rows, you can just buffer the entire response into a single row.&lt;/P&gt;</description>
    <pubDate>Wed, 19 Dec 2012 05:28:58 GMT</pubDate>
    <dc:creator>ineeman</dc:creator>
    <dc:date>2012-12-19T05:28:58Z</dc:date>
    <item>
      <title>REST API - JSON Invalid format</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36208#M6601</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;
I was trying to get the data from Splunk using curl REST API with the following detail:-&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;curl -k -u myusername:mypassword -d search="search index%3Dmain sourcetype%3Dmain-st | dedup Name | table Name, _geo" -d earliest_time=-24h@h -d output_mode="json" &lt;A href="https://myurl:8089/servicesNS/admin/maps/search/jobs/export" target="test_blank"&gt;https://myurl:8089/servicesNS/admin/maps/search/jobs/export&lt;/A&gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The result was,&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{"preview":true,"offset":0,"result":{"Name":"mamycita","_geo":"7.13,120.193"}}
{"preview":true,"offset":0,"result":{"Name":"gogocita","_geo":"7.13,120.193"}}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Upon checking in &lt;A href="http://jsonlint.com/"&gt;http://jsonlint.com/&lt;/A&gt;, the result from Splunk was not a valid JSON format.&lt;BR /&gt;
,&lt;/P&gt;

&lt;P&gt;Is there anything we need to do to get the valid json data via REST API? Or Did I make any mistake in the code?&lt;/P&gt;

&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2012 10:11:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36208#M6601</guid>
      <dc:creator>panglimajalak</dc:creator>
      <dc:date>2012-11-15T10:11:28Z</dc:date>
    </item>
    <item>
      <title>Re: REST API - JSON Invalid format</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36209#M6602</link>
      <description>&lt;P&gt;Each line of the preview is valid JSON, you just need to split it up.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2012 10:19:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36209#M6602</guid>
      <dc:creator>martin_mueller</dc:creator>
      <dc:date>2012-11-15T10:19:41Z</dc:date>
    </item>
    <item>
      <title>Re: REST API - JSON Invalid format</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36210#M6603</link>
      <description>&lt;P&gt;You also have the choice to use &lt;CODE&gt;json_rows&lt;/CODE&gt; or &lt;CODE&gt;json_cols&lt;/CODE&gt; as output modes. The output for these is formatted in rows and columns respectively and it will be one big block of valid json string.&lt;/P&gt;

&lt;P&gt;You can also look at some example usage of these output modes in the &lt;A href="http://dev.splunk.com/view/javascript-sdk/SP-CAAAECM"&gt;Splunk JavaScript SDK&lt;/A&gt; - &lt;A href="http://dev.splunk.com/view/SP-CAAAEDC"&gt;Examples&lt;/A&gt; section. &lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2012 16:33:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36210#M6603</guid>
      <dc:creator>Neeraj_Luthra</dc:creator>
      <dc:date>2012-11-15T16:33:29Z</dc:date>
    </item>
    <item>
      <title>Re: REST API - JSON Invalid format</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36211#M6604</link>
      <description>&lt;P&gt;It's not a valid JSON document if "each line is valid JSON". Fundamentally, no json parser can parse this response - which is the whole point of returning JSON, so it's easy to parse. Having to pre-parse a JSON response defeats the whole purpose. &lt;/P&gt;

&lt;P&gt;I opened a case with Splunk support and they've indicated that they have reproduced the issue and that it is indeed returning invalid JSON. A fix should hopefully be forthcoming. &lt;/P&gt;</description>
      <pubDate>Thu, 29 Nov 2012 22:50:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36211#M6604</guid>
      <dc:creator>aarondnichols</dc:creator>
      <dc:date>2012-11-29T22:50:52Z</dc:date>
    </item>
    <item>
      <title>Re: REST API - JSON Invalid format</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36212#M6605</link>
      <description>&lt;P&gt;Note: the only reason you get an "invalid" document is because you are using the /export endpoint. This is not an endpoint you should generally be using unless you have a very specific set of requirements (namely, you need to export a large amount of data out of Splunk).&lt;/P&gt;

&lt;P&gt;Your observation that the response as a whole is not a valid JSON document is correct. However, this is by design, and equivalent to what we do with XML output. Let me try and explain our rationale.&lt;/P&gt;

&lt;P&gt;The idea behind the /export endpoint is that it is a streaming endpoint, which means it will send out results as soon as they are available. However, this is complicated for searches that never "end", like real-time searches, for example. It is also complicated for searches for which we want to give you previews of the results (the above two are really the same problem, but that's a detail).&lt;/P&gt;

&lt;P&gt;In order to support the streaming nature of the endpoint, we need to be able to give you the data in a format that is very easy to parse in a continuous, streaming fashion. That is why we decided to give each single row as an individual JSON object, where rows are guaranteed to be separated by a newline, and we will disclose whenever a "preview" is closed. For example, here is a sample search invocation, and the resulting output:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;$ curl -k -u admin:changeme &lt;A href="https://localhost:8089/services/search/jobs/export" target="test_blank"&gt;https://localhost:8089/services/search/jobs/export&lt;/A&gt; --data-urlencode search="search index=_internal | stats count by sourcetype" -d output_mode=json -d earliest="rt-5m" -d latest="rt"

{"preview":true,"offset":0,"result":{"sourcetype":"eventgen-2","count":"58509"}}
{"preview":true,"offset":1,"result":{"sourcetype":"splunk_web_service","count":"119"}}
{"preview":true,"offset":2,"result":{"sourcetype":"splunkd","count":"4153"}}
{"preview":true,"offset":3,"result":{"sourcetype":"splunkd_access","count":"12"}}
{"preview":true,"offset":4,"lastrow":true,"result":{"sourcetype":"splunkd_stderr","count":"2"}}
{"preview":true,"offset":0,"result":{"sourcetype":"eventgen-2","count":"60886"}}
{"preview":true,"offset":1,"result":{"sourcetype":"splunk_web_service","count":"119"}}
{"preview":true,"offset":2,"result":{"sourcetype":"splunkd","count":"4280"}}
{"preview":true,"offset":3,"result":{"sourcetype":"splunkd_access","count":"12"}}
{"preview":true,"offset":4,"lastrow":true,"result":{"sourcetype":"splunkd_stderr","count":"2"}}
{"preview":true,"offset":0,"result":{"sourcetype":"eventgen-2","count":"63342"}}
{"preview":true,"offset":1,"result":{"sourcetype":"splunk_web_service","count":"119"}}
{"preview":true,"offset":2,"result":{"sourcetype":"splunkd","count":"4404"}}
{"preview":true,"offset":3,"result":{"sourcetype":"splunkd_access","count":"12"}}
{"preview":true,"offset":4,"lastrow":true,"result":{"sourcetype":"splunkd_stderr","count":"2"}}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now, as you can see, we actually have three previews in here, each with 4 rows (the reason these are all previews is that this is a real-time search, which only has "previews"). We know when each preview ended by looking at the the "lastrow" field in each individual line.&lt;/P&gt;

&lt;P&gt;So, as an example, let's say I wanted to get each preview as a single array of objects, it would look like this, in pseudo-code:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;// Run the search
splunkResultStream = splunkAPI.exportSearch(....);

// Have a place to buffer the entire preview
currentPreview = []

// OK, let's go
while(true) {
    event = splunkResultStream.readUntilNewline();

    // Always append to the preview        
    currentPreview.append(event);

    // If it is the last row, we can actually do something!
    if (event.lastrow) {
        doSomethingWithEntirePreview(currentPreview)

        // And now we start a new preview
        currentPreview = []
    }
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The above is pretty straightforward, but you might say "why can't we just have a single JSON object for each preview?" That's a reasonable question, and our answer is that these objects are of unbounded size, and JSON is not easily parsed in a streaming option, so we want to give you the option of parsing it in an easy way, while also providing flexibility. For example, there are many use cases where you do not need to buffer an entire preview and actually just care about each row individually, and this can be very performant.&lt;/P&gt;

&lt;P&gt;By the way, if this format looks familiar to you, that is on purpose. Many streaming-based APIs use it, the most popular being the Twitter Streaming API.&lt;/P&gt;

&lt;P&gt;Hopefully this explains why the format is the way it is. If you have any more questions or need some help in dealing with it, please let us know.&lt;/P&gt;</description>
      <pubDate>Fri, 30 Nov 2012 19:39:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36212#M6605</guid>
      <dc:creator>ineeman</dc:creator>
      <dc:date>2012-11-30T19:39:33Z</dc:date>
    </item>
    <item>
      <title>Re: REST API - JSON Invalid format</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36213#M6606</link>
      <description>&lt;P&gt;So whats the best way to parse the result set? right now  I only have about 10-15 rows that return. IS there different methods depending on how large the result set it?&lt;/P&gt;

&lt;P&gt;thanks&lt;/P&gt;</description>
      <pubDate>Tue, 18 Dec 2012 22:11:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36213#M6606</guid>
      <dc:creator>cgiatras</dc:creator>
      <dc:date>2012-12-18T22:11:33Z</dc:date>
    </item>
    <item>
      <title>Re: REST API - JSON Invalid format</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36214#M6607</link>
      <description>&lt;P&gt;If you have 10-15 rows and none of them are previews, then you can simply split on newlines (&lt;CODE&gt;\n&lt;/CODE&gt;), and then parse each string as a JSON object and meld them together. If some of them are previews, basically do the same, just ignore anything that has the &lt;CODE&gt;preview&lt;/CODE&gt; property set to &lt;CODE&gt;true&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;Note that since you have so few rows, you can just buffer the entire response into a single row.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Dec 2012 05:28:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36214#M6607</guid>
      <dc:creator>ineeman</dc:creator>
      <dc:date>2012-12-19T05:28:58Z</dc:date>
    </item>
    <item>
      <title>Re: REST API - JSON Invalid format</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36215#M6608</link>
      <description>&lt;P&gt;This is disappointing. At the very least, you should have the option of specifying to stream results as they are available or to return a single, valid, json document.  If the endpoint doesn't return a valid document, then the endpoint is broken, plain and simple.&lt;/P&gt;

&lt;P&gt;Additionally, the engineering team at some point seems to have reused this same functionality to provide json exporting via the web interface.  This means that after performing a search and then attempting to download, the downloaded file (via HTTP, web client, no streaming) is broken.  Sure, I can reparse it and fix it, but then I'm only being reminded that the tool is broken in the first place.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Feb 2015 15:47:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36215#M6608</guid>
      <dc:creator>scrown</dc:creator>
      <dc:date>2015-02-27T15:47:11Z</dc:date>
    </item>
    <item>
      <title>Re: REST API - JSON Invalid format</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36216#M6609</link>
      <description>&lt;P&gt;My 2 cents: if you intentionally output invalid JSON, why not just not output in JSON-like at all? So that people would have less confusion and can workaround accordingly.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2017 02:15:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36216#M6609</guid>
      <dc:creator>leonjxtan</dc:creator>
      <dc:date>2017-06-12T02:15:25Z</dc:date>
    </item>
    <item>
      <title>Re: REST API - JSON Invalid format</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36217#M6610</link>
      <description>&lt;P&gt;I also agree with this, it is broken if its not a proper JSON payload. It needs a simple array around it and make it , delimeted per json row.&lt;/P&gt;

&lt;P&gt;I ended up fixing this in my python - i have attached the fix below : &lt;/P&gt;

&lt;H1&gt;!/bin/python3.6&lt;/H1&gt;

&lt;P&gt;import json&lt;BR /&gt;
from collections import defaultdict&lt;/P&gt;

&lt;P&gt;class GetDetails():&lt;BR /&gt;
    def &lt;STRONG&gt;init&lt;/STRONG&gt;(self):&lt;BR /&gt;
        self.load = self.setup_details('file.json')&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;def setup_details(self, filename):
    data = open(filename).read()
    count = data.count("}}") - 1
    data = data.replace("}}", "}},", count)
    data = data.replace("}}\n", "}}]\n")
    data = data.replace('{', '[{', 1)
    newdata = json.loads(data)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Then you can use in another def to run through the actuals, not ideal SPLUNK to have a bad json format. &lt;/P&gt;</description>
      <pubDate>Mon, 03 Jun 2019 03:19:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36217#M6610</guid>
      <dc:creator>paulsilver2000</dc:creator>
      <dc:date>2019-06-03T03:19:09Z</dc:date>
    </item>
    <item>
      <title>Re: REST API - JSON Invalid format</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36218#M6611</link>
      <description>&lt;P&gt;This is also an issue, having a non-completed JSON output is not ideal for an API call. I ended up having to write up some code to wrap around this issue, so we could consume it. I have posted the python3 code here - so you can consume it if you have a similar issue, i had created a script to wrap around the lack of ability that SPLUNK offers in this simple requirement.&lt;/P&gt;

&lt;P&gt;import json&lt;BR /&gt;
from collections import defaultdict&lt;/P&gt;

&lt;P&gt;class GetDetailsForEmail():&lt;BR /&gt;
    def &lt;STRONG&gt;init&lt;/STRONG&gt;(self):&lt;BR /&gt;
        self.load = self.setup_details('file.json')&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;def setup_details(self, filename):
    data = open(filename).read()
    count = data.count("}}") - 1
    data = data.replace("}}", "}},", count)
    data = data.replace("}}\n", "}}]\n")
    data = data.replace('{', '[{', 1)
    newdata = json.loads(data)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Jun 2019 03:22:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36218#M6611</guid>
      <dc:creator>paulsilver2000</dc:creator>
      <dc:date>2019-06-03T03:22:26Z</dc:date>
    </item>
    <item>
      <title>Re: REST API - JSON Invalid format</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36219#M6612</link>
      <description>&lt;P&gt;I found this to work a little more consistently -&lt;/P&gt;

&lt;P&gt;def fixSplunkJSON( strSplunk ) :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;strJSON = strSplunk.replace( '}}', '}},' )
strJSON = strJSON.replace( '}}\n', '}}' )
strJSON = strJSON.rstrip( '\n' )
strJSON = strJSON.rstrip( ',' )
strJSON = '[' + strJSON + ']'
return strJSON
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Sep 2019 17:24:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36219#M6612</guid>
      <dc:creator>stephenwmccoy</dc:creator>
      <dc:date>2019-09-12T17:24:58Z</dc:date>
    </item>
    <item>
      <title>Re: REST API - JSON Invalid format</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36220#M6613</link>
      <description>&lt;P&gt;Struggling with this, can you shed some more light on this.&lt;BR /&gt;
I am outputting an API result and it looks like this on the command line&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{"preview":false,"offset":13,"result":{"Time":"17:55 Aug 13 2019","client-note":"Test event"}}
{"preview":false,"offset":14,"result":{"Time":"22:55 Aug 12 2019","client-note":"testing Android"}}
{"preview":false,"offset":15,"result":{"Time":"21:05 Aug 12 2019","client-note":"testing Hello"}}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This API is going to be executed externally from a mobile device and the result needs to be valid json&lt;BR /&gt;
ie comma separated not newline&lt;/P&gt;

&lt;P&gt;Can you explain how I could apply your def to this action so the output is formatted correctly&lt;/P&gt;

&lt;P&gt;Cheers&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 06:28:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36220#M6613</guid>
      <dc:creator>proylea</dc:creator>
      <dc:date>2019-09-17T06:28:44Z</dc:date>
    </item>
    <item>
      <title>Re: REST API - JSON Invalid format</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36221#M6614</link>
      <description>&lt;P&gt;Good point. I suspect nothing has been changed so far.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 20:05:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36221#M6614</guid>
      <dc:creator>highsplunker</dc:creator>
      <dc:date>2019-11-25T20:05:50Z</dc:date>
    </item>
    <item>
      <title>Re: REST API - JSON Invalid format</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36222#M6615</link>
      <description>&lt;P&gt;we would need to remove the header and footer of the json file that you have received via rest api. So that, it will be structured as json. use SEDCMD regex in props.conf. Even am looking for help how can i generate the regex.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 10:02:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/REST-API-JSON-Invalid-format/m-p/36222#M6615</guid>
      <dc:creator>bala_poluru</dc:creator>
      <dc:date>2020-02-27T10:02:57Z</dc:date>
    </item>
  </channel>
</rss>

