# Transform Typescript query of user sessions from @dynatrace-sdk/client-classic-environment-v1 in Notebooks and Dashboards to JSON

**URL:** <https://community.dynatrace.com/t/transform-typescript-query-of-user-sessions-from-dynatrace-sdk-client-classic-environment-v1-in-notebooks-and-dashboards-to-json/41003>\
**Category:** DQL\
**Tags:** dql, typescript, usql, notebooks, dashboards, user-sessions\
**Created:** [August 2, 2023, 6:13pm UTC](https://community.dynatrace.com/t/transform-typescript-query-of-user-sessions-from-dynatrace-sdk-client-classic-environment-v1-in-notebooks-and-dashboards-to-json/41003 "2023-08-02T18:13:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![heybeckerj](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/heybeckerj/32/22189_2.png) [@heybeckerj](https://community.dynatrace.com/u/heybeckerj)\
**Post date:** [August 2, 2023, 6:13pm UTC](https://community.dynatrace.com/t/transform-typescript-query-of-user-sessions-from-dynatrace-sdk-client-classic-environment-v1-in-notebooks-and-dashboards-to-json/41003/1 "2023-08-02T18:13:12Z")

</div>

SDK query used:&nbsp;[Classic environment v1 | Dynatrace Developer](https://developer.dynatrace.com/reference/sdks/client-classic-environment-v1/#getusqlresultastable)

I have an api query that returns the result as a json payload with a field for columns and values (records) as arrays:

```javascript
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;

}

```

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:

Here is the documentation for the sdk query result type/promise:&nbsp;[Classic environment v1 | Dynatrace Developer](https://developer.dynatrace.com/reference/sdks/client-classic-environment-v1/#usqlresultastable)

Example response :

The fields are in the columnNames array and the records are in the values array of arrays:

| columnNames | Array\<[string](https://developer.mozilla.org/en-US/docs/Glossary/String)\> | A list of columns in the result table. |
| values | Array\<Array\<[any](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any)\>\> | A list of data rows.Each array element represents a row in the result table.The size of each data row and the order of the elements correspond to the&nbsp; **columnNames** &nbsp;content. |

Example response :

```markup
{
  "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"
    ]
  ]
}

```

How can I transform this result into a usable JSON table that can be used in visualisations?

Even record list does not yeild the intended result:

 ![heybeckerj_0-1690999950791.png](https://global.discourse-cdn.com/dynatrace/original/3X/c/6/c6544f71f7bdde333495164597a91cc792bab09b.png)

---

<div class="post-metadata">

**Author:** ![rkdy](https://avatars.discourse-cdn.com/v4/letter/r/cab0a1/32.png) [@rkdy](https://community.dynatrace.com/u/rkdy)\
**Post date:** [August 3, 2023, 12:58am UTC](https://community.dynatrace.com/t/transform-typescript-query-of-user-sessions-from-dynatrace-sdk-client-classic-environment-v1-in-notebooks-and-dashboards-to-json/41003/2 "2023-08-03T00:58:06Z")

</div>

The following could be a possible **workaround** 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:

```javascript
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 => {
    return {
      [columnNames[0]]: row[0],
      [columnNames[1]]: row[1]
    };
  });

  // console.table(table);

  return table;
}

```

---

<div class="post-metadata">

**Author:** ![Fin\_Ubels](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/fin_ubels/32/18008_2.png) [@Fin\_Ubels](https://community.dynatrace.com/u/Fin_Ubels)\
**Post date:** [August 3, 2023, 2:03am UTC](https://community.dynatrace.com/t/transform-typescript-query-of-user-sessions-from-dynatrace-sdk-client-classic-environment-v1-in-notebooks-and-dashboards-to-json/41003/3 "2023-08-03T02:03:46Z")

</div>

Hey Heybeckerj,

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.

For the above query I came up with the following to make it work in table format:

```javascript
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;

}

```

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.

 ![Fin_Ubels_0-1691028159728.png](https://global.discourse-cdn.com/dynatrace/original/3X/8/4/844ff67d21ce5d0926d7422a27a147984ec30972.png)

 ![Fin_Ubels_1-1691028208234.png](https://global.discourse-cdn.com/dynatrace/original/2X/9/997068663a36c2b9e93d1a090f0a0bb7cbd600ae.png)

Hope this helps!

---

<div class="post-metadata">

**Author:** ![heybeckerj](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/heybeckerj/32/22189_2.png) [@heybeckerj](https://community.dynatrace.com/u/heybeckerj)\
**Post date:** [August 3, 2023, 5:38pm UTC](https://community.dynatrace.com/t/transform-typescript-query-of-user-sessions-from-dynatrace-sdk-client-classic-environment-v1-in-notebooks-and-dashboards-to-json/41003/4 "2023-08-03T17:38:38Z")

</div>

Thank you&nbsp;[@Fin\_Ubels](https://community.dynatrace.com/user/47376)&nbsp;and&nbsp;[@rkdy](https://community.dynatrace.com/user/54274)&nbsp;, both of these great suggestions!

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.

Example:

```javascript
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 => {
        let obj = {};
        // add each element of the row to the object with the corresponding key
        row.forEach((value, index) => {
            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;

}

```

Result:

 ![heybeckerj_0-1691084393703.png](https://global.discourse-cdn.com/dynatrace/original/3X/f/0/f048b75d2e3d613ea7059c886c1af4403dc502b9.png)

But, now there are 3 documentated ways to do this. I’ve learned so much! 👍💪🛠🤘🚀
