<?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: Python REST API JSON response malformed on one instance in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/Python-REST-API-JSON-response-malformed-on-one-instance/m-p/518705#M9271</link>
    <description>&lt;P class="lia-indent-padding-left-30px"&gt;"&lt;SPAN&gt;can you try removing regex and see if the order is same"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Not easily because that would require the app to be re-exported and re-installed, the latter of which requires a reboot but it's a production system.&lt;/P&gt;&lt;P&gt;In any case, the problem seems to be at the Python stage which is before the regex stage so I'm not sure that's relevant.&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;"&lt;SPAN&gt;are you using json module in python ?&lt;/SPAN&gt;"&lt;/P&gt;&lt;P&gt;Not exactly. We're using &lt;FONT face="courier new,courier"&gt;response.json()&lt;/FONT&gt; from the module &lt;FONT face="courier new,courier"&gt;requests&lt;/FONT&gt;.&lt;/P&gt;</description>
    <pubDate>Wed, 09 Sep 2020 13:45:41 GMT</pubDate>
    <dc:creator>benhooper</dc:creator>
    <dc:date>2020-09-09T13:45:41Z</dc:date>
    <item>
      <title>Python REST API JSON response malformed on one instance</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Python-REST-API-JSON-response-malformed-on-one-instance/m-p/518682#M9269</link>
      <description>&lt;P&gt;We've used the Add-on Builder to create a custom app which uses a Python script to query a REST API, process some of the data (mostly to convert epoch to human-readable timestamps), and write events to Splunk.&lt;/P&gt;&lt;P&gt;This works&amp;nbsp;fine on three different test or development instances.&amp;nbsp;&lt;SPAN&gt;On those, the returned data look like the following:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2020-09-09 13-32-55 - New_Message_-_Splunk_Community_-_Google_Chrome.png" style="width: 733px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/10747i908A4FE6B93BB33E/image-size/large?v=v2&amp;amp;px=999" role="button" title="2020-09-09 13-32-55 - New_Message_-_Splunk_Community_-_Google_Chrome.png" alt="2020-09-09 13-32-55 - New_Message_-_Splunk_Community_-_Google_Chrome.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The API's documentation and manually running the API request in Python confirms that this is the normal and expected data structure:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2020-09-09 13-36-52 - New_Message_-_Splunk_Community_-_Google_Chrome.png" style="width: 735px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/10748i1077079F897AFED7/image-size/large?v=v2&amp;amp;px=999" role="button" title="2020-09-09 13-36-52 - New_Message_-_Splunk_Community_-_Google_Chrome.png" alt="2020-09-09 13-36-52 - New_Message_-_Splunk_Community_-_Google_Chrome.png" /&gt;&lt;/span&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;As such, the regex for field parsing / extraction is written to follow this structure.&lt;/P&gt;&lt;P&gt;However, when we run the same version of the app on the&amp;nbsp;production instance there are two problems with the returned data:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;The data is in a completely different order. This is an unworkable problem with regex and I don't want to have to maintain a separate version just for this once instance.&lt;/LI&gt;&lt;LI&gt;Every key is prefixed with &lt;FONT face="courier new,courier"&gt;u&lt;/FONT&gt;. I guess that, for some reason, this is to explicitly define the strings as Unicode but, whatever the reason, I guess that using &lt;FONT face="courier new,courier"&gt;u*&lt;/FONT&gt;&amp;nbsp;would work around this fairly easily.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2020-09-09 13-45-22.png" style="width: 733px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/10749iCD1EEE45FE71E45A/image-size/large?v=v2&amp;amp;px=999" role="button" title="2020-09-09 13-45-22.png" alt="2020-09-09 13-45-22.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Does anyone know why this is happening?&lt;/P&gt;&lt;P&gt;Further information on the instances' environments:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;1 x development:&lt;UL&gt;&lt;LI&gt;OS: Ubuntu Server 20.04&lt;/LI&gt;&lt;LI&gt;Splunk Enterprise: 8.0.5.&lt;/LI&gt;&lt;LI&gt;Python: 3.8.2&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;UL&gt;&lt;LI&gt;2 x test:&lt;UL&gt;&lt;LI&gt;OS: Ubuntu Server 20.04&lt;/LI&gt;&lt;LI&gt;Splunk Enterprise: 8.0.5.&lt;/LI&gt;&lt;LI&gt;Python: 3.8.2&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;LI&gt;Production:&lt;UL&gt;&lt;LI&gt;OS: Ubuntu Server 18.04.4 LTS&lt;/LI&gt;&lt;LI&gt;Splunk Enterprise: 8.0.4.&lt;/LI&gt;&lt;LI&gt;Python: 3.6.9&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Update 2020/09/10 11:32:&lt;/STRONG&gt; I just tried running the API commands in Python on the actual production instance and it worked fine so it seems to be Splunk itself that's causing this problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Update 2020/09/11 16:03:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;On the production instance, I updated the installation of Splunk Enterprise to version 8.0.6 (latest as of writing) but it didn't make a difference.&lt;/P&gt;&lt;P&gt;Interestingly enough, when the custom app is installed via the Splunk Add-on Builder, rather than directly, it works fine and exactly as expected, even though it's installed directly on the test instances.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Sep 2020 15:05:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Python-REST-API-JSON-response-malformed-on-one-instance/m-p/518682#M9269</guid>
      <dc:creator>benhooper</dc:creator>
      <dc:date>2020-09-11T15:05:16Z</dc:date>
    </item>
    <item>
      <title>Re: Python REST API JSON response malformed on one instance</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Python-REST-API-JSON-response-malformed-on-one-instance/m-p/518703#M9270</link>
      <description>&lt;OL&gt;&lt;LI&gt;The data is in a completely different order. This is an unworkable problem with regex&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;can you try removing regex and see if the order is same.&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Every key is prefixed with&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;u&lt;/FONT&gt;. I guess that, for some reason, this is to explicitly define the strings as Unicode but, whatever the reason, I guess that using&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;u*&lt;/FONT&gt;&amp;nbsp;would work around this fairly easily.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;are you using json module in python ?&lt;/P&gt;&lt;P&gt;if not use json module and do below before further processing your response:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;json_loads = json.loads(response.content) # this should solve the issue I guess.
json_dumps = json.dumps(json_loads) # try adding this also to above if above it selft doesn't work.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://stackoverflow.com/questions/13940272/python-json-loads-returns-items-prefixing-with-u" target="_blank"&gt;https://stackoverflow.com/questions/13940272/python-json-loads-returns-items-prefixing-with-u&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Sep 2020 13:38:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Python-REST-API-JSON-response-malformed-on-one-instance/m-p/518703#M9270</guid>
      <dc:creator>thambisetty</dc:creator>
      <dc:date>2020-09-09T13:38:13Z</dc:date>
    </item>
    <item>
      <title>Re: Python REST API JSON response malformed on one instance</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Python-REST-API-JSON-response-malformed-on-one-instance/m-p/518705#M9271</link>
      <description>&lt;P class="lia-indent-padding-left-30px"&gt;"&lt;SPAN&gt;can you try removing regex and see if the order is same"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Not easily because that would require the app to be re-exported and re-installed, the latter of which requires a reboot but it's a production system.&lt;/P&gt;&lt;P&gt;In any case, the problem seems to be at the Python stage which is before the regex stage so I'm not sure that's relevant.&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;"&lt;SPAN&gt;are you using json module in python ?&lt;/SPAN&gt;"&lt;/P&gt;&lt;P&gt;Not exactly. We're using &lt;FONT face="courier new,courier"&gt;response.json()&lt;/FONT&gt; from the module &lt;FONT face="courier new,courier"&gt;requests&lt;/FONT&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Sep 2020 13:45:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Python-REST-API-JSON-response-malformed-on-one-instance/m-p/518705#M9271</guid>
      <dc:creator>benhooper</dc:creator>
      <dc:date>2020-09-09T13:45:41Z</dc:date>
    </item>
    <item>
      <title>Re: Python REST API JSON response malformed on one instance</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Python-REST-API-JSON-response-malformed-on-one-instance/m-p/519471#M9273</link>
      <description>&lt;P&gt;I added the following lines to the Python script:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;pythonversion = str(sys.version_info[0]) + "." + str(sys.version_info[1]) + "." + str(sys.version_info[2])
helper.log_info("collect_events() triggered. Currently running Python version {}.".format(pythonversion))&lt;/LI-CODE&gt;&lt;P&gt;From this, I discovered that the app was being run in Python version 2.7 but it was designed for Python version 3.&lt;/P&gt;&lt;P&gt;I:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Added the line&amp;nbsp;&lt;FONT face="courier new,courier"&gt;python.version = python3&lt;/FONT&gt; under the section&amp;nbsp;&lt;FONT face="courier new,courier"&gt;[general]&lt;/FONT&gt; in file&amp;nbsp;&lt;FONT face="courier new,courier"&gt;/opt/splunk/etc/system/local/server.conf&lt;/FONT&gt;&lt;/LI&gt;&lt;LI&gt;Removed the app with command&amp;nbsp;&lt;FONT face="courier new,courier"&gt;sudo /opt/splunk/bin/splunk remove app &amp;lt;appName&amp;gt; &lt;FONT face="arial,helvetica,sans-serif"&gt;which deleted the index(es).&lt;/FONT&gt;&lt;/FONT&gt;&lt;/LI&gt;&lt;LI&gt;Deleted the app's KV store with command&amp;nbsp;&lt;FONT face="courier new,courier"&gt;sudo /opt/splunk/bin/splunk clean kvstore -app &amp;lt;appName&amp;gt;&lt;/FONT&gt; (just in case)&lt;/LI&gt;&lt;LI&gt;Restarted Splunk.&lt;/LI&gt;&lt;LI&gt;Reinstalled the app.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;The REST API then worked as expected.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2020 15:09:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Python-REST-API-JSON-response-malformed-on-one-instance/m-p/519471#M9273</guid>
      <dc:creator>benhooper</dc:creator>
      <dc:date>2020-09-14T15:09:20Z</dc:date>
    </item>
  </channel>
</rss>

