Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8127359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:30:03+00:00 2026-06-06T07:30:03+00:00

I thought the following would be a pretty common task and assumed there would

  • 0

I thought the following would be a pretty common task and assumed there would be an easy solution for it, but i can’t find one.

If I have a datatable in the following structure.

ID  Name    Active
ID1 John    TRUE
ID2 Bill    FALSE

I would like to serialize it as a JSON object where the ID column is a node in the JSON object like:

[
    {
        "ID1": {
            "Name": "John",
            "Active": "True"
        },
        "ID2": {
            "Name": "Bill",
            "Active": "False"
        }
    }
]

I looked into JSON.NET but could not get it to work.
Edit: I’m using C#

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:30:06+00:00Added an answer on June 6, 2026 at 7:30 am

    This is quite simple with JSON.NET. Just convert your data table into the equivalent dictionary of dictionaries:

    public Dictionary<string, Dictionary<string, object>> DatatableToDictionary(DataTable dt, string id)
    {
        var cols = dt.Columns.Cast<DataColumn>().Where(c => c.ColumnName != id);
        return dt.Rows.Cast<DataRow>()
                 .ToDictionary(r => r[id].ToString(), 
                               r => cols.ToDictionary(c => c.ColumnName, c => r[c.ColumnName]));
    }
    

    Then call:

    JsonConvert.SerializeObject(DatatableToDictionary(dt, "ID"), Newtonsoft.Json.Formatting.Indented);
    

    Here’s the full test:

    var dt = new DataTable("MyTable");
    dt.Columns.Add("ID");
    dt.Columns.Add("Name");
    dt.Columns.Add("Active");
    
    dt.LoadDataRow(new[] {"ID1", "John", "True"}, true);
    dt.LoadDataRow(new[] {"ID2", "Bill", "False"}, true);
    
    JsonConvert.SerializeObject(DatatableToDictionary(dt, "ID"));
    

    And the result:

    {
      "ID1": {
        "Name": "John",
        "Active": "True"
      },
      "ID2": {
        "Name": "Bill",
        "Active": "False"
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought this would be pretty simple, but guess not. I have the following
I've looked around but have yet to find a great solution the the following
I thought I understood sed but I guess not. I have the following two
I thought this would be pretty academic but its not. I'm trying to traverse
How can one accomplish class-based default value in following scheme? I mean, I would
I'm pretty sure that many people have thought of this, but for some reason
This is what I thought would be a simple select clause, however the following
I thought the following script will create div element but I got nothing output
I thought the following line of code should work fine: $(.1).attr('href', '#Home'); Right? But
Never thought I'd have this problem :) The following snippet of code works in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.