<?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: Why does REST API return nested json as string not json? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Why-does-REST-API-return-nested-json-as-string-not-json/m-p/630243#M218960</link>
    <description>&lt;P&gt;You are not wrong. &amp;nbsp;&lt;SPAN&gt;&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/JSONFunctions#json_object.28.26lt.3Bmembers.26gt.3B.29" target="_blank" rel="noopener"&gt;json_object()&lt;/A&gt; creates an escaped JSON string in order to protect the object in ordinarily text contexts. &amp;nbsp;Many log files use this technique, especially when they are already a JSON object but need to embed a text message that includes JSON objects. &amp;nbsp;You just need to remember to unescape it when you need to access JSON.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;SPL functions in a text context. &amp;nbsp;Therefore it automatically un-excapes strings created by this function. spath readily recognizes such un-escaped string. &amp;nbsp;You can examine this example,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| fields - _time
| eval my_message = json_object("key","value")
| spath input=my_message&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output will be&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;key&lt;/TD&gt;&lt;TD&gt;my_message&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;value&lt;/TD&gt;&lt;TD&gt;{"key":"value"}&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
    <pubDate>Thu, 09 Feb 2023 08:41:22 GMT</pubDate>
    <dc:creator>yuanliu</dc:creator>
    <dc:date>2023-02-09T08:41:22Z</dc:date>
    <item>
      <title>Why does REST API return nested json as string not json?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-REST-API-return-nested-json-as-string-not-json/m-p/629827#M218809</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I am using the REST API to pull data from splunk, using the output_mode=json.&lt;BR /&gt;&lt;BR /&gt;The data that is returned is a mix of strings and JSON (objects) and I am trying to work out a way for the API to return the entire data set as JSON.&lt;BR /&gt;&lt;BR /&gt;For Example:&lt;BR /&gt;&lt;STRONG&gt;Curl Command:&lt;/STRONG&gt;&lt;BR /&gt;curl -k -u 'user1'' &lt;A href="https://splunk-server:8089/servicesNS/admin/search/search/jobs/export" target="_blank" rel="noopener"&gt;https://splunk-server:8089/servicesNS/admin/search/search/jobs/export&lt;/A&gt; -d 'preview=false' -d 'output_mode=json' -d 'search=|savedsearch syslog_stats latest="-2d@d" earliest="-3d@d" span=1' | jq .&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Results&lt;/STRONG&gt;: Note how the result is in JSON, but devices is an array of strings not json.&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; "preview": false,&lt;BR /&gt;&amp;nbsp; "offset": 0,&lt;BR /&gt;&amp;nbsp; "lastrow": true,&lt;BR /&gt;&amp;nbsp; "result": {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; "MsgType": "LINK-3-UPDOWN",&lt;BR /&gt;&amp;nbsp; &amp;nbsp; "devices": [&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; "{\"device\":\"1.1.1.1\",\"events\":12,\"deviceId\":null}",&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; "{\"device\":\"2.2.2.2\",\"events\":128,\"deviceId\":1}",&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; "{\"device\":\"3.3.3.3\",\"events\":217,\"deviceId\":2}"&lt;BR /&gt;&amp;nbsp; &amp;nbsp; ],&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; "total": "357",&lt;BR /&gt;&amp;nbsp; }&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Query:&lt;/STRONG&gt;&lt;BR /&gt;| tstats count as events where index=X-syslog Severity&amp;lt;=4 earliest=-3d@d latest=-2d@d by _time, Severity, MsgType Device span=1d&lt;BR /&gt;| search MsgType="LINK-3-UPDOWN"&lt;BR /&gt;| eval devices=&lt;STRONG&gt;json_object&lt;/STRONG&gt;("device", Device, "events", events, "deviceId", deviceId )&lt;BR /&gt;| fields - Device events _time Filter UUID Regex deviceId addressDeviceId&lt;BR /&gt;| table MsgType devices&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Query Result in UI:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;MsgType devices total&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;LINK-3-UPDOWN&lt;/TD&gt;
&lt;TD&gt;
&lt;DIV class=""&gt;{"device":"1.1.1.1","events":12,"deviceId":null}&lt;/DIV&gt;
&lt;DIV class=""&gt;{"device":"2.2.2.2","events":128,"deviceId":null}&lt;/DIV&gt;
&lt;DIV class=""&gt;{"device":"3.3.3.3","events":217,"deviceId":null}&lt;/DIV&gt;
&lt;/TD&gt;
&lt;TD&gt;357&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As can be seen from the UI the device is in JSON format (using json_object), but from the curl result it is a string in json format - is there a way for the query to return the whole result as a json object, not a mix of json and strings ?&lt;BR /&gt;&lt;BR /&gt;I have also tried tojson in a number of differnt ways, but no success.&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Desired Result: &lt;/STRONG&gt;where devices is a json object and not treated a string as above.&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; "preview": false,&lt;BR /&gt;&amp;nbsp; "offset": 0,&lt;BR /&gt;&amp;nbsp; "lastrow": true,&lt;BR /&gt;&amp;nbsp; "result": {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; "MsgType": "LINK-3-UPDOWN",&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;EM&gt;&lt;STRONG&gt;"devices": [&lt;/STRONG&gt;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {"device":"1.1.1.1","events":12,"deviceId":null}",&lt;/STRONG&gt;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {"device":"2.2.2.2","events":128,"deviceId":1}",&lt;/STRONG&gt;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {"device":"3.3.3.3","events":217,"deviceId":2}"&lt;/STRONG&gt;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ],&lt;/STRONG&gt;&lt;/EM&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; "total": "357",&lt;BR /&gt;&amp;nbsp; }&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;I can post process the strings into JSON, but I would rather get JSON from SPlunk directly.&lt;BR /&gt;&lt;BR /&gt;Thanks !&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2023 21:24:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-REST-API-return-nested-json-as-string-not-json/m-p/629827#M218809</guid>
      <dc:creator>bdunstan</dc:creator>
      <dc:date>2023-02-07T21:24:46Z</dc:date>
    </item>
    <item>
      <title>Re: Why does REST API return nested json as string not json?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-REST-API-return-nested-json-as-string-not-json/m-p/630072#M218884</link>
      <description>&lt;P&gt;That is because the original data field "devices" contain strings and &lt;STRONG&gt;not&lt;/STRONG&gt; JSON objects. (Even though the strings are themselves escaped JSON objects.) &amp;nbsp;For the output to be like your desired results, search UI will give you this instead:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="64.515625px" height="25px"&gt;lastrow&lt;/TD&gt;&lt;TD width="53.890625px" height="25px"&gt;offset&lt;/TD&gt;&lt;TD width="68.421875px" height="25px"&gt;preview&lt;/TD&gt;&lt;TD width="120.59375px" height="25px"&gt;result.MsgType&lt;/TD&gt;&lt;TD width="173.375px" height="25px"&gt;&lt;DIV class=""&gt;result.devices{}.device&lt;/DIV&gt;&lt;/TD&gt;&lt;TD width="129.015625px" height="25px"&gt;&lt;DIV class=""&gt;result.devices{}.deviceId&lt;/DIV&gt;&lt;/TD&gt;&lt;TD width="119.140625px" height="25px"&gt;&lt;DIV class=""&gt;result.devices{}.events&lt;/DIV&gt;&lt;/TD&gt;&lt;TD width="56.0625px" height="25px"&gt;results.total&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="64.515625px" height="69px"&gt;true&lt;/TD&gt;&lt;TD width="53.890625px" height="69px"&gt;0&lt;/TD&gt;&lt;TD width="68.421875px" height="69px"&gt;false&lt;/TD&gt;&lt;TD width="120.59375px" height="69px"&gt;LINK-3-UPDOWN&lt;/TD&gt;&lt;TD width="173.375px" height="69px"&gt;&lt;DIV class=""&gt;1.1.1.1&lt;/DIV&gt;&lt;DIV class=""&gt;2.2.2.2&lt;/DIV&gt;&lt;DIV class=""&gt;3.3.3.3&lt;/DIV&gt;&lt;/TD&gt;&lt;TD width="129.015625px" height="69px"&gt;&lt;DIV class=""&gt;null&lt;/DIV&gt;&lt;DIV class=""&gt;1&lt;/DIV&gt;&lt;DIV class=""&gt;2&lt;/DIV&gt;&lt;/TD&gt;&lt;TD width="119.140625px" height="69px"&gt;&lt;DIV class=""&gt;12&lt;/DIV&gt;&lt;DIV class=""&gt;128&lt;/DIV&gt;&lt;DIV class=""&gt;217&lt;/DIV&gt;&lt;/TD&gt;&lt;TD width="56.0625px" height="69px"&gt;357&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Escaped JSON string is often employed by considerate developers to avoid unnecessary complexity.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2023 11:02:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-REST-API-return-nested-json-as-string-not-json/m-p/630072#M218884</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2023-02-08T11:02:42Z</dc:date>
    </item>
    <item>
      <title>Re: Why does REST API return nested json as string not json?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-REST-API-return-nested-json-as-string-not-json/m-p/630186#M218933</link>
      <description>&lt;P&gt;Thanks.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I make the assumtion (obviously wrong) that the json_object() command created a JSON object and not a string that looks like JSON.&lt;BR /&gt;&lt;BR /&gt;| eval devices=&lt;STRONG&gt;json_object&lt;/STRONG&gt;("device", Device, "events", events, "deviceId", deviceId )&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2023 20:56:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-REST-API-return-nested-json-as-string-not-json/m-p/630186#M218933</guid>
      <dc:creator>bdunstan</dc:creator>
      <dc:date>2023-02-08T20:56:36Z</dc:date>
    </item>
    <item>
      <title>Re: Why does REST API return nested json as string not json?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-REST-API-return-nested-json-as-string-not-json/m-p/630243#M218960</link>
      <description>&lt;P&gt;You are not wrong. &amp;nbsp;&lt;SPAN&gt;&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/JSONFunctions#json_object.28.26lt.3Bmembers.26gt.3B.29" target="_blank" rel="noopener"&gt;json_object()&lt;/A&gt; creates an escaped JSON string in order to protect the object in ordinarily text contexts. &amp;nbsp;Many log files use this technique, especially when they are already a JSON object but need to embed a text message that includes JSON objects. &amp;nbsp;You just need to remember to unescape it when you need to access JSON.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;SPL functions in a text context. &amp;nbsp;Therefore it automatically un-excapes strings created by this function. spath readily recognizes such un-escaped string. &amp;nbsp;You can examine this example,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| fields - _time
| eval my_message = json_object("key","value")
| spath input=my_message&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output will be&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;key&lt;/TD&gt;&lt;TD&gt;my_message&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;value&lt;/TD&gt;&lt;TD&gt;{"key":"value"}&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Thu, 09 Feb 2023 08:41:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-REST-API-return-nested-json-as-string-not-json/m-p/630243#M218960</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2023-02-09T08:41:22Z</dc:date>
    </item>
  </channel>
</rss>

