<?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: Transform Typescript query of user sessions from @dynatrace-sdk/client-classic-environment-v1 in Notebooks and Dashboards to J in DQL</title>
    <link>https://community.dynatrace.com/t5/DQL/Transform-Typescript-query-of-user-sessions-from-dynatrace-sdk/m-p/219719#M169</link>
    <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://community.dynatrace.com/t5/user/viewprofilepage/user-id/47376"&gt;@Fin_Ubels&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://community.dynatrace.com/t5/user/viewprofilepage/user-id/54274"&gt;@rkdy&lt;/a&gt;&amp;nbsp;, both of these great suggestions!&lt;/P&gt;&lt;P&gt;I managed to find a way to reconstruct a table form the result by iterating through the 2D array object as well as the columnNames array object instead of having the hardcode any headers. This way it is more dynamic.&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;import { rumUserSessionsClient } from "@dynatrace-sdk/client-classic-environment-v1";

function convertTableToJSON(headers,table) {
    // extract header as keys
    let keys = headers;
    // create JSON objects from the remaining data 
    let json = table.map(row =&amp;gt; {
        let obj = {};
        // add each element of the row to the object with the corresponding key
        row.forEach((value, index) =&amp;gt; {
            obj[keys[index]] = value;
        });
        return obj;
    });
    return json;
}

export default async function () {
  
const response = await rumUserSessionsClient.getUsqlResultAsTable({
  query: "SELECT DISTINCT city, country FROM usersession",
  startTimestamp: 1688184000000 ,
  endTimestamp: 0,
  pageOffset: 0
});
 console.log(response);
  const jsonData = convertTableToJSON(response.columnNames,response.values);
  console.log(jsonData);
  return jsonData;

}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Result:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="heybeckerj_0-1691084393703.png" style="width: 400px;"&gt;&lt;img src="https://community.dynatrace.com/t5/image/serverpage/image-id/13351i07B662AB4DDFB06D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="heybeckerj_0-1691084393703.png" alt="heybeckerj_0-1691084393703.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But, now there are 3 documentated ways to do this. I've learned so much! &lt;span class="lia-unicode-emoji" title=":thumbs_up:"&gt;👍🏼&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":flexed_biceps:"&gt;💪🏼&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":hammer_and_wrench:"&gt;🛠&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":sign_of_the_horns:"&gt;🤘&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":rocket:"&gt;🚀&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 03 Aug 2023 17:40:21 GMT</pubDate>
    <dc:creator>heybeckerj</dc:creator>
    <dc:date>2023-08-03T17:40:21Z</dc:date>
    <item>
      <title>Transform Typescript query of user sessions from @dynatrace-sdk/client-classic-environment-v1 in Notebooks and Dashboards to JSON</title>
      <link>https://community.dynatrace.com/t5/DQL/Transform-Typescript-query-of-user-sessions-from-dynatrace-sdk/m-p/219610#M166</link>
      <description>&lt;P&gt;SDK query used:&amp;nbsp;&lt;A href="https://developer.dynatrace.com/reference/sdks/client-classic-environment-v1/#getusqlresultastable" target="_blank" rel="noopener"&gt;https://developer.dynatrace.com/reference/sdks/client-classic-environment-v1/#getusqlresultastable&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN class=""&gt;I have an api query that returns the result as a json payload with a field for columns and values (records) as arrays:&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;import { rumUserSessionsClient } from "@dynatrace-sdk/client-classic-environment-v1";


export default async function () {
  
const response = await rumUserSessionsClient.getUsqlResultAsTable({
  query: "SELECT DISTINCT city, country FROM usersession",
  startTimestamp: 0 ,
  endTimestamp: 0,
  pageOffset: 0
});


  return response;

}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN class=""&gt;Im attempting to get this into JSON. However, the response is not valid JSON but rather arrays of arrays for the records and an array for columnNames:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;Here is the documentation for the sdk query result type/promise:&amp;nbsp;&lt;A href="https://developer.dynatrace.com/reference/sdks/client-classic-environment-v1/#usqlresultastable" target="_blank" rel="noopener"&gt;https://developer.dynatrace.com/reference/sdks/client-classic-environment-v1/#usqlresultastable&lt;/A&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;Example response :&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;The fields are in the columnNames array and the records are in the values array of arrays:&lt;/SPAN&gt;&lt;/P&gt;&lt;TABLE width="486px"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="112px" height="30px"&gt;columnNames&lt;/TD&gt;&lt;TD width="110px" height="30px"&gt;Array&amp;lt;&lt;A href="https://developer.mozilla.org/en-US/docs/Glossary/String" target="_blank" rel="noopener noreferrer"&gt;string&lt;/A&gt;&amp;gt;&lt;/TD&gt;&lt;TD width="264px" height="30px"&gt;A list of columns in the result table.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="112px"&gt;values&lt;/TD&gt;&lt;TD width="110px"&gt;Array&amp;lt;Array&amp;lt;&lt;A href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any" target="_blank" rel="noopener noreferrer"&gt;any&lt;/A&gt;&amp;gt;&amp;gt;&lt;/TD&gt;&lt;TD width="264px"&gt;&lt;P&gt;A list of data rows.&lt;/P&gt;&lt;P&gt;Each array element represents a row in the result table.&lt;/P&gt;&lt;P&gt;The size of each data row and the order of the elements correspond to the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;columnNames&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;content.&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;Example response :&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{
  "extrapolationLevel": 1,
  "columnNames": [
    "city",
    "country"
  ],
  "values": [
    [
      "Burlington",
      "Canada"
    ],
    [
      "Cary",
      "United States"
    ],
    [
      "Johannesburg",
      "South Africa"
    ],
    [
      "Los Angeles",
      "United States"
    ],
    [
      "Manchester",
      "United Kingdom"
    ],
    [
      "Northampton",
      "United Kingdom"
    ],
    [
      "Stoke-on-Trent",
      "United Kingdom"
    ]
  ]
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;How can I transform this result into a usable JSON table that can be used in visualisations?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Even record list does not yeild the intended result:&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="heybeckerj_0-1690999950791.png" style="width: 400px;"&gt;&lt;img src="https://community.dynatrace.com/t5/image/serverpage/image-id/13318iC0201936750453F6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="heybeckerj_0-1690999950791.png" alt="heybeckerj_0-1690999950791.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Aug 2023 18:13:12 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/DQL/Transform-Typescript-query-of-user-sessions-from-dynatrace-sdk/m-p/219610#M166</guid>
      <dc:creator>heybeckerj</dc:creator>
      <dc:date>2023-08-02T18:13:12Z</dc:date>
    </item>
    <item>
      <title>Re: Transform Typescript query of user sessions from @dynatrace-sdk/client-classic-environment-v1 in Notebooks and Dashboards to J</title>
      <link>https://community.dynatrace.com/t5/DQL/Transform-Typescript-query-of-user-sessions-from-dynatrace-sdk/m-p/219629#M167</link>
      <description>&lt;P&gt;The following could be a possible &lt;STRONG&gt;workaround&lt;/STRONG&gt; for this specific example, unsure if the output you are see is what is expected going forward where you are not mapping the values manually:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;import { rumUserSessionsClient } from "@dynatrace-sdk/client-classic-environment-v1";

export default async function () {
  const response = await rumUserSessionsClient.getUsqlResultAsTable({
    query: "SELECT DISTINCT city, country FROM usersession",
    startTimestamp: 0 ,
    endTimestamp: 0,
    pageOffset: 0
  });

  // Extracting the column names and values from the response
  const columnNames = response.columnNames;
  const values = response.values;

  // Creating a table-like structure
  const table = values.map(row =&amp;gt; {
    return {
      [columnNames[0]]: row[0],
      [columnNames[1]]: row[1]
    };
  });

  // console.table(table);

  return table;
}
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2023 00:59:13 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/DQL/Transform-Typescript-query-of-user-sessions-from-dynatrace-sdk/m-p/219629#M167</guid>
      <dc:creator>rkdy</dc:creator>
      <dc:date>2023-08-03T00:59:13Z</dc:date>
    </item>
    <item>
      <title>Re: Transform Typescript query of user sessions from @dynatrace-sdk/client-classic-environment-v1 in Notebooks and Dashboards to J</title>
      <link>https://community.dynatrace.com/t5/DQL/Transform-Typescript-query-of-user-sessions-from-dynatrace-sdk/m-p/219634#M168</link>
      <description>&lt;P&gt;Hey Heybeckerj,&lt;/P&gt;
&lt;P&gt;To achieve the desired outcome you will need to reformat the response so that it can be parsed correctly. The best way to see how a response should be formatted for the tiles is by running a regular DQL query and viewing it in the raw format.&lt;/P&gt;
&lt;P&gt;For the above query I came up with the following to make it work in table format:&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;import { rumUserSessionsClient } from "@dynatrace-sdk/client-classic-environment-v1";


export default async function () {
  
  const response = await rumUserSessionsClient.getUsqlResultAsTable({
    query: "SELECT DISTINCT city, country FROM usersession",
    startTimestamp: 1688343399000,
    endTimestamp: 1691021800000,
    pageOffset: 0
  });

  response.values.forEach(myFunction)
  
  function myFunction(item, index, arr) {
    arr[index] = { 'city' : item[0], 'country' : item[1] }
  }


  return response.values;

}&lt;/LI-CODE&gt;
&lt;P&gt;What this is doing is returning just the values but also looping through them all and adding the column name 'city' and 'country' as well as reformatting all of the records to be objects instead of arrays.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Fin_Ubels_0-1691028159728.png" style="width: 400px;"&gt;&lt;img src="https://community.dynatrace.com/t5/image/serverpage/image-id/13330i84438A4B2D778499/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Fin_Ubels_0-1691028159728.png" alt="Fin_Ubels_0-1691028159728.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Fin_Ubels_1-1691028208234.png" style="width: 400px;"&gt;&lt;img src="https://community.dynatrace.com/t5/image/serverpage/image-id/13331i13380BF7179CF5A0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Fin_Ubels_1-1691028208234.png" alt="Fin_Ubels_1-1691028208234.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps!&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2023 02:03:46 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/DQL/Transform-Typescript-query-of-user-sessions-from-dynatrace-sdk/m-p/219634#M168</guid>
      <dc:creator>Fin_Ubels</dc:creator>
      <dc:date>2023-08-03T02:03:46Z</dc:date>
    </item>
    <item>
      <title>Re: Transform Typescript query of user sessions from @dynatrace-sdk/client-classic-environment-v1 in Notebooks and Dashboards to J</title>
      <link>https://community.dynatrace.com/t5/DQL/Transform-Typescript-query-of-user-sessions-from-dynatrace-sdk/m-p/219719#M169</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://community.dynatrace.com/t5/user/viewprofilepage/user-id/47376"&gt;@Fin_Ubels&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://community.dynatrace.com/t5/user/viewprofilepage/user-id/54274"&gt;@rkdy&lt;/a&gt;&amp;nbsp;, both of these great suggestions!&lt;/P&gt;&lt;P&gt;I managed to find a way to reconstruct a table form the result by iterating through the 2D array object as well as the columnNames array object instead of having the hardcode any headers. This way it is more dynamic.&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;import { rumUserSessionsClient } from "@dynatrace-sdk/client-classic-environment-v1";

function convertTableToJSON(headers,table) {
    // extract header as keys
    let keys = headers;
    // create JSON objects from the remaining data 
    let json = table.map(row =&amp;gt; {
        let obj = {};
        // add each element of the row to the object with the corresponding key
        row.forEach((value, index) =&amp;gt; {
            obj[keys[index]] = value;
        });
        return obj;
    });
    return json;
}

export default async function () {
  
const response = await rumUserSessionsClient.getUsqlResultAsTable({
  query: "SELECT DISTINCT city, country FROM usersession",
  startTimestamp: 1688184000000 ,
  endTimestamp: 0,
  pageOffset: 0
});
 console.log(response);
  const jsonData = convertTableToJSON(response.columnNames,response.values);
  console.log(jsonData);
  return jsonData;

}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Result:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="heybeckerj_0-1691084393703.png" style="width: 400px;"&gt;&lt;img src="https://community.dynatrace.com/t5/image/serverpage/image-id/13351i07B662AB4DDFB06D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="heybeckerj_0-1691084393703.png" alt="heybeckerj_0-1691084393703.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But, now there are 3 documentated ways to do this. I've learned so much! &lt;span class="lia-unicode-emoji" title=":thumbs_up:"&gt;👍🏼&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":flexed_biceps:"&gt;💪🏼&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":hammer_and_wrench:"&gt;🛠&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":sign_of_the_horns:"&gt;🤘&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":rocket:"&gt;🚀&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2023 17:40:21 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/DQL/Transform-Typescript-query-of-user-sessions-from-dynatrace-sdk/m-p/219719#M169</guid>
      <dc:creator>heybeckerj</dc:creator>
      <dc:date>2023-08-03T17:40:21Z</dc:date>
    </item>
  </channel>
</rss>

