Sibeesh Passion

Top Menu

  • Home
  • Search
  • About
  • Privacy Policy

Main Menu

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Home
  • Search
  • About
  • Privacy Policy

logo

Sibeesh Passion

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment

  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins

  • Post Messages to Microsoft Teams Using Python

  • Get Azure Blob Storage Blob Metadata Using PowerShell

  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines

.NETASP.NETC#JQueryJsonMicrosoft ADOMD
Home›.NET›Convert Microsoft ADOMD Cell Set to JSON

Convert Microsoft ADOMD Cell Set to JSON

By SibeeshVenu
March 29, 2015
2083
0
Share:

In this article we will learn how we can convert Microsoft ADOMD cell sets to JSON. There are few possible ways to convert a CellSet to JSON, Here by I am sharing you one among that options. We will create a function which accepts cellset as the argument and we will loop through the axes of cellset and bind the values according to a string builder using a JsonWriter class. I hope you will like it.

Background

For the past few months I have been working with Microsoft ADOMD data sources. And I have written some article also that will describe the problems I have encountered so far. If you are new to ADOMD I strongly recommend that read my previous articles that you may find useful when you work with ADOMD data sources. You can find those article links here.

  • How to Convert Microsoft ADOMD Data Source to JSON
  • Convert CellSet to HTML Table and From HTML to JSON and to Array
  • Why

    You might think, why am I again using the methods described in the preceding two articles. I will answer that. I have encountered some issues with those methods. When you use a data adapter or data reader as explained in the first link (How to Convert Microsoft ADOMD Data Source to JSON) you always get the values as normal values instead of formatted values. For example even if the value contains $ or %, you will always get values without those symbols. So your application won’t let the user identify which one is currency or which one is %. In my case it was high chart and high maps. When the user hovers over a specific area, I need to show the measure values in the tooltip.

    So in that case I was forced to use the cell set again, where there is an option that we can select the formatted value. I will show you that in my function.

    Using the code

    The following is the function that does what was explained above.
    [csharp]
    private string BuildBubbleMap(CellSet cst)
    {
    try
    {
    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    string columnName = string.Empty;
    string fieldVal = string.Empty;
    //check if any axes were returned else throw error.
    int axes_count = cst.Axes.Count;
    if (axes_count == 0)
    throw new Exception("No data returned for the selection");

    //if axes count is not 2
    if (axes_count != 2)
    throw new Exception("The code support only queries with two axes");

    //if no position on either row or column throw error
    if (!(cst.Axes[0].Positions.Count > 0) && !(cst.Axes[1].Positions.Count > 0))
    throw new Exception("No data returned for the selection");

    int cur_row, cur_col, col_count, row_count, col_dim_count, row_dim_count;
    row_dim_count = 0;

    //Number of dimensions on the column
    col_dim_count = cst.Axes[0].Positions[0].Members.Count;

    //Number of dimensions on the row
    if (cst.Axes[1].Positions.Count > 0)
    {
    if (cst.Axes[1].Positions[0].Members.Count > 0)
    row_dim_count = cst.Axes[1].Positions[0].Members.Count;
    }
    //Total rows and columns
    row_count = cst.Axes[1].Positions.Count + col_dim_count; //number of rows + rows for column headers
    col_count = cst.Axes[0].Positions.Count + row_dim_count; //number of columns + columns for row headers

    using (JsonWriter myJson = new JsonTextWriter(sw))
    {
    myJson.WriteStartArray();
    for (cur_row = 0; cur_row < row_count-1; cur_row++)
    {
    myJson.WriteStartObject();
    for (cur_col = 0; cur_col < col_count-1; cur_col++)
    {
    //Looping for dimension headers
    columnName = cst.Axes[1].Positions[cur_row].Members[cur_col].ParentLevel.ToString().Replace("{", "").Replace("}", "").Trim();
    fieldVal = cst.Axes[1].Positions[cur_row].Members[cur_col].Caption.Replace(",", " ");
    //If the value is null, I dont need that to be included
    if ((columnName == null || columnName == "" || columnName.ToLower() == "undefined" || columnName.ToLower() == "null" ||
    columnName.ToLower() == "(null)" || columnName.ToLower() == "unknown")||(fieldVal == null || fieldVal == "" ||
    fieldVal.ToLower() == "undefined" || fieldVal.ToLower() == "null" ||
    fieldVal.ToLower() == "(null)" || fieldVal.ToLower() == "unknown"))
    break;
    //Map expect the header as lat and lon, so here we are changing that.
    if (columnName.ToLower() == "latitude")
    columnName = "lat";
    else if (columnName.ToLower() == "longitude")
    columnName = "lon";

    myJson.WritePropertyName(columnName);
    myJson.WriteValue(fieldVal);
    }
    //Looping for measure headers
    myJson.WritePropertyName(cst.Axes[0].Positions[0].Members[0].Caption.Replace(",", " ").Trim());
    myJson.WriteValue(cst[cur_row].FormattedValue);
    //Please be noted that we are using FormattedValue here.
    myJson.WriteEndObject();
    }
    myJson.WriteEndArray();
    }
    cst = null;
    return sw.ToString();
    }
    catch (Exception)
    {
    cst = null;
    throw;
    }
    }
    [/csharp]

    You can see that this function expects the parameter cell set. Here we are finding the axis (Axis0 and Axis 1). According to my ADOMD query my dimensions are in Axis 1, so that I need to determine the caption for each dimension from the cell set. Once I determine the dimensions I am writing that to the StringBuilder using the method WritePropertyName() of the JSON Writer class. To use this class you must include the Newtonsoft DLL as in the following.
    [csharp]
    using Newtonsoft.Json;
    [/csharp]

    Once that is over, I am writing the value for those dimensions using the WriteValue() method of the JSON writer class.

    I am doing that for the measures also. You can see these implementations in my code. Finally I am creating the proper JSON here and returning that.

    To use this function you need to build the cell set first. You can do that as follows.
    [csharp]
    using (AdomdConnection conn = new AdomdConnection(myConnection))
    {
    conn.Open();
    using (AdomdCommand cmd = new AdomdCommand(query, conn))
    {
    cmd.CommandTimeout = connectionTimeout;
    cst = cmd.ExecuteCellSet();
    }
    }
    [/csharp]

    Once you get the cell set, just pass the cell set to the preceding function, it will return the JSON you need. You can easily bind it to any client-side tool (for example, High Chart and High Maps).

    Conclusion

    Have you ever gone through this kind of requirement. Did I miss anything that you may think which is needed?. I hope you liked this article. Please share me your valuable suggestions and feedback.

    Your turn. What do you think?

    A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C-Sharp Corner, Stack Overflow, Asp.Net Forums or Code Project instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I am able to.

    Kindest Regards
    Sibeesh Venu

    TagsADOMDADOMD CellSetCellSet JSONConvert CellSet to JSONConvert Microsoft ADOMD Cell Set to JSONJson
    Previous Article

    Dynamically load & check whether it is ...

    Next Article

    Creating Custom Color Palette Using JQuery, HTML5, ...

    0
    Shares
    • 0
    • +
    • 0
    • 0
    • 0

    SibeeshVenu

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer.

    Related articles More from author

    • JQWidgetsJQX GridProducts

      Bind Json Data to JQWidget JQX Grid

      June 18, 2015
      By SibeeshVenu
    • .NETASP.NETC#JsonMicrosoft ADOMD

      How to Convert Microsoft ADOMD Data Source to JSON

      January 29, 2015
      By SibeeshVenu
    • CodeProjectJQueryJson

      Sort a JSON Array Programmatically by a Property

      June 10, 2015
      By SibeeshVenu
    • JSON in Textarea
      How toJQuery

      Style Or Format JSON Data In JQuery

      May 31, 2016
      By SibeeshVenu
    • Q&A

      CellSetGrid Implementation in VS 2012 or later

      July 14, 2015
      By SibeeshVenu
    • JQueryJson

      Find JSON Objects With Same Property and Separate Them

      May 29, 2015
      By SibeeshVenu
    0

    My book

    Asp Net Core and Azure with Raspberry Pi Sibeesh Venu

    YouTube

    MICROSOFT MVP (2016-2022)

    profile for Sibeesh Venu - Microsoft MVP

    Recent Posts

    • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment
    • Build, Deploy, Configure CI &CD Your Static Website in 5 mins
    • Easily move data from one COSMOS DB to another
    • .NET 8 New and Efficient Way to Check IP is in Given IP Range
    • Async Client IP safelist for Dot NET
    • Post Messages to Microsoft Teams Using Python
    • Get Azure Blob Storage Blob Metadata Using PowerShell
    • Deploy .net 6 App to Azure from Azure DevOps using Pipelines
    • Integrate Azure App Insights in 1 Minute to .Net6 Application
    • Azure DevOps Service Connection with Multiple Azure Resource Group

    Tags

    Achievements (35) Angular (14) Angular 5 (7) Angular JS (15) article (10) Article Of The Day (13) Asp.Net (14) Azure (65) Azure DevOps (10) Azure Function (10) Azure IoT (7) C# (17) c-sharp corner (13) Career Advice (11) chart (11) CSharp (7) CSS (7) CSS3 (6) HighChart (10) How To (9) HTML5 (10) HTML5 Chart (11) Interview (6) IoT (11) Javascript (10) JQuery (82) jquery functions (9) JQWidgets (15) JQX Grid (17) Json (7) Microsoft (8) MVC (20) MVP (9) MXChip (7) News (18) Office 365 (7) Products (10) SQL (20) SQL Server (15) Visual Studio (10) Visual Studio 2017 (7) VS2017 (7) Web API (12) Windows 10 (7) Wordpress (9)
    • .NET
    • Achievements
    • ADO.NET
    • Android
    • Angular
    • Arduino
    • Article Of The Day
    • ASP.NET
    • Asp.Net Core
    • Automobile
    • Awards
    • Azure
    • Azure CDN
    • azure devops
    • Blockchain
    • Blog
    • Browser
    • C-Sharp Corner
    • C#
    • Career Advice
    • Code Snippets
    • CodeProject
    • Cognitive Services
    • Cosmos DB
    • CSS
    • CSS3
    • Data Factory
    • Database
    • Docker
    • Drawings
    • Drill Down Chart
    • English
    • Excel Programming
    • Exporting
    • Facebook
    • Fun
    • Gadgets
    • GitHub
    • GoPro
    • High Map
    • HighChart
    • How to
    • HTML
    • HTML5
    • Ignite UI
    • IIS
    • Interview
    • IoT
    • JavaScript
    • JQuery
    • jQuery UI
    • JQWidgets
    • JQX Grid
    • Json
    • Knockout JS
    • Linux
    • Machine Learning
    • Malayalam
    • Malayalam Poems
    • MDX Query
    • Microsoft
    • Microsoft ADOMD
    • Microsoft MVP
    • Microsoft Office
    • Microsoft Technologies
    • Microsoft Windows
    • Microsoft Windows Server
    • Mobile
    • MongoDB
    • Monthly Winners
    • MVC
    • MVC Grid
    • MySQL
    • News
    • Node JS
    • npm
    • Number Conversions
    • October 2015
    • Office 365
    • Office Development
    • One Plus
    • Outlook
    • Page
    • PHP
    • Poems
    • PowerShell
    • Products
    • Q&A
    • Raspberry PI
    • React
    • SEO
    • SharePoint
    • Skype
    • Social Media
    • Software
    • Spire.Doc
    • Spire.PDF
    • Spire.XLS
    • SQL
    • SQL Server
    • SSAS
    • SSMS
    • Storage In HTML5
    • Stories
    • Third Party Software Apps
    • Tips
    • Tools
    • Translator Text
    • Uncategorized
    • Unit Testing
    • UWP
    • VB.Net
    • Videos
    • Virtual Machine
    • Visual Studio
    • Visual Studio 2017
    • Wamp Server
    • Web API
    • Web Platform Installer
    • Webinars
    • WebMatrix
    • Windows 10
    • Windows 7
    • Windows 8.1
    • Wordpress
    • Writing

    ABOUT ME

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer. If you would like to know more about me, you can read my story here.

    Contact Me

    • info@sibeeshpassion.com

    Pages

    • About
    • Search
    • Privacy Policy
    • About
    • Search
    • Privacy Policy
    © Copyright Sibeesh Passion 2014-2025. All Rights Reserved.
    Go to mobile version