0

I have a SQL Server source table defined as:

CstCmpCode       Main_Group         Sub_Group         ClosBal
AH01                    Neck Wraps        AFGHANI              7

I need the output to be:

{
  "CstCmpCode": "AH01",
  "Main_Group": "Neck Wraps",
  "sub_group": [
    {
      "Sub_Group": "AFGHANI",
      "ClosBal": 7
    }
  ]
}

I have the source SQL Server table being imported to a DataSet, then was going to use JSON.NET to parse the results. I was thinking I should create some sort of Class structure with a main_group having a list of sub_group/closbal key/value pairs, but I'm not totally sure if that's the right track or not.

Thanks. Yogesh.Sharma

2 Answers 2

2

you can directly do it by using following code(with Newtonsoft Json.NET).

string JSONresult;
JSONresult = JsonConvert.SerializeObject(yourDataTable);  
Sign up to request clarification or add additional context in comments.

2 Comments

but, my json string is not getting upgdated with nested json string
In this case your approach is the right one, for reference your can use this link. stackoverflow.com/questions/15628569/nested-json-from-datatable
0

If you need this output you should have a class like this:

public class SubGroup
{
    public string Sub_Group { get; set; }
    public int ClosBal { get; set; }
}

public class RootObject
{
    public string CstCmpCode { get; set; }
    public string Main_Group { get; set; }
    public List<SubGroup> sub_group { get; set; }
}

An objects of this class can be serialized to the JSON format you mentioned.

1 Comment

directly it is possible ? without implementing the for loop for datatable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.