<?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: Splunk REST API: Issue wtih flattening JSON-formatted results in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Splunk-REST-API-Issue-wtih-flattening-JSON-formatted-results/m-p/348492#M64021</link>
    <description>&lt;P&gt;Why are you pulling data from the API in a SPL command?&lt;/P&gt;

&lt;P&gt;The results in the pipeline are readily available to you.&lt;/P&gt;

&lt;P&gt;Are you taking the results of one search and running another based on them?&lt;/P&gt;</description>
    <pubDate>Fri, 04 Aug 2017 18:21:40 GMT</pubDate>
    <dc:creator>jkat54</dc:creator>
    <dc:date>2017-08-04T18:21:40Z</dc:date>
    <item>
      <title>Splunk REST API: Issue wtih flattening JSON-formatted results</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Splunk-REST-API-Issue-wtih-flattening-JSON-formatted-results/m-p/348491#M64020</link>
      <description>&lt;P&gt;So I call the Splunk REST API and collect results in JSON format and that is kind of okay.&lt;BR /&gt;
Then I would like to pass it to  &lt;CODE&gt;splunk.Intersplunk.outputResults()&lt;/CODE&gt;&lt;BR /&gt;
Intersplunk fails to flatten this kind of complex object, so a workaround would be to just get the subset data["entry"] and one leven nesting gets flattened nicely. &lt;BR /&gt;
However, there are nested elements in that as well like ACL, links, etc.&lt;BR /&gt;
The nested elements show up as python object in string representation, which you can not then (as a lazy way out) use  &lt;CODE&gt;spath&lt;/CODE&gt; on...&lt;BR /&gt;
Has anyone found a good way to do this transformation?&lt;/P&gt;

&lt;P&gt;example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{
  "links": {

  },
  "generator": {
    "build": "debde650d26e",
    "version": "6.4.1"
  },
  "updated": "2017-08-04T15:19:21+02:00",
  "origin": "https://xxx:8089/servicesNS/nobody/-/",
  "entry": [
    {
      "links": {
        "alternate": "/servicesNS/nobody/SplunkUniversalForwarder/"
      },
      "name": "SplunkUniversalForwarder",
      "updated": "2017-08-04T15:19:21+02:00",
      "id": "https://xxx:8089/servicesNS/nobody/SplunkUniversalForwarder/"
    },
    {
      "links": {
        "alternate": "/servicesNS/nobody/alert_logevent/"
      },
      "name": "alert_logevent",
      "updated": "2017-08-04T15:19:21+02:00",
      "id": "https://xxx:8089/servicesNS/nobody/alert_logevent/"
    },
    {
      "links": {
        "alternate": "/servicesNS/nobody/introspection_generator_addon/"
      },
      "name": "introspection_generator_addon",
      "updated": "2017-08-04T15:19:21+02:00",
      "id": "https://xxx:8089/servicesNS/nobody/introspection_generator_addon/"
    }
  ]
}
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Aug 2017 15:38:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Splunk-REST-API-Issue-wtih-flattening-JSON-formatted-results/m-p/348491#M64020</guid>
      <dc:creator>dominiquevocat</dc:creator>
      <dc:date>2017-08-04T15:38:20Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk REST API: Issue wtih flattening JSON-formatted results</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Splunk-REST-API-Issue-wtih-flattening-JSON-formatted-results/m-p/348492#M64021</link>
      <description>&lt;P&gt;Why are you pulling data from the API in a SPL command?&lt;/P&gt;

&lt;P&gt;The results in the pipeline are readily available to you.&lt;/P&gt;

&lt;P&gt;Are you taking the results of one search and running another based on them?&lt;/P&gt;</description>
      <pubDate>Fri, 04 Aug 2017 18:21:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Splunk-REST-API-Issue-wtih-flattening-JSON-formatted-results/m-p/348492#M64021</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2017-08-04T18:21:40Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk REST API: Issue wtih flattening JSON-formatted results</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Splunk-REST-API-Issue-wtih-flattening-JSON-formatted-results/m-p/348493#M64022</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;import splunk.Intersplunk

# get the keywords suplied to the curl command
keywords, options = splunk.Intersplunk.getKeywordsAndOptions()

# get the previous search results
results,dummyresults,settings = splunk.Intersplunk.getOrganizedResults()

if len(results) &amp;gt; 0:
  for result in results:
    #result["fieldName"] is the field value in one row of your results
    result["fieldName"]="NEW VALUE"

else:
  #no data in pipe
  row={}
  results=[]
  row["foo"] = "bar"
  row["foo2"] = "bar2"
  results.append(row)

splunk.Intersplunk.outputResults(results)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Aug 2017 18:30:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Splunk-REST-API-Issue-wtih-flattening-JSON-formatted-results/m-p/348493#M64022</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2017-08-04T18:30:17Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk REST API: Issue wtih flattening JSON-formatted results</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Splunk-REST-API-Issue-wtih-flattening-JSON-formatted-results/m-p/348494#M64023</link>
      <description>&lt;P&gt;for the usecase see &lt;A href="https://splunkbase.splunk.com/app/2775/"&gt;https://splunkbase.splunk.com/app/2775/&lt;/A&gt; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2017 09:02:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Splunk-REST-API-Issue-wtih-flattening-JSON-formatted-results/m-p/348494#M64023</guid>
      <dc:creator>dominiquevocat</dc:creator>
      <dc:date>2017-08-07T09:02:38Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk REST API: Issue wtih flattening JSON-formatted results</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Splunk-REST-API-Issue-wtih-flattening-JSON-formatted-results/m-p/348495#M64024</link>
      <description>&lt;P&gt;In that case just use the Python json library, and iterate through dumping strings into results (example of creating results provided in my comments above).&lt;/P&gt;

&lt;P&gt;&lt;A href="https://docs.python.org/2/library/json.html"&gt;https://docs.python.org/2/library/json.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2017 11:50:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Splunk-REST-API-Issue-wtih-flattening-JSON-formatted-results/m-p/348495#M64024</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2017-08-07T11:50:01Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk REST API: Issue wtih flattening JSON-formatted results</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Splunk-REST-API-Issue-wtih-flattening-JSON-formatted-results/m-p/348496#M64025</link>
      <description>&lt;P&gt;Seems to be mostly good enough:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;        if pagehandle.status_code==200:
            data = json.loads(pagehandle.text)
            if "entry" in data:
                results = data["entry"]
                for row in results:
                    #decorate each list item with fields from the returned data
                    if "origin" in data: row["origin"]=data["origin"]
                    if "generator" in data: 
                        row["version"]=data["generator"]["version"]
                        row["build"]=data["generator"]["build"]
                    if "paging" in data: 
                        row["paging:total"]=data["paging"]["total"]
                        row["paging:offset"]=data["paging"]["offset"]
                        row["paging:perPage"]=data["paging"]["perPage"]

                    #flatten notable data
                    if "acl" in row:
                        for acl in row["acl"]:
                            aclitem = str(acl)
                            row[aclitem] = row["acl"][acl]
                        row.pop("acl", None)
                        #alternatively return it as json                        
                        #row["acl"] = json.dumps(row["acl"])
                        #row["acl"].replace("\"", "'")
                    if "perms" in row:
                        if row["perms"] is not None: # there might be "None" values
                            for perm in row["perms"]:
                                permitem = str(perm)
                                row[permitem] = row["perms"][perm]
                            row.pop("perms", None)
                            #alternatively return it as json                        
                            #row["acl"] = json.dumps(row["acl"])
                            #row["acl"].replace("\"", "'")
                    if "content" in row:
                        for content in row["content"]:
                            contentitem = str(content)
                            row[contentitem] = row["content"][content]
                        row.pop("content", None)
                        #alternatively return it as json
                        #row["content"] = json.dumps(row["content"])
                        #row["content"].replace("\"", "'")
                    if "links" in row:
                        for link in row["links"]:
                            linkitem = str(link)
                            row[linkitem] = row["links"][link]
                        row.pop("links", None)
                        #alternatively return it as json
                        #row["links"] = json.dumps(row["links"])
                        #row["links"].replace("\"", "'")
                splunk.Intersplunk.outputResults(results)
            else:
                splunk.Intersplunk.generateErrorResults("nothing to show")
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Aug 2017 11:06:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Splunk-REST-API-Issue-wtih-flattening-JSON-formatted-results/m-p/348496#M64025</guid>
      <dc:creator>dominiquevocat</dc:creator>
      <dc:date>2017-08-08T11:06:00Z</dc:date>
    </item>
  </channel>
</rss>

