473,382 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Build JSON with two datatable values

My question is how can I combine two sets of JSON for example one set comes from one datatable like this:

"ID": 1, "SHORT_NAME": "B", "CARRIER_NAME": "Carrier A"

Another one with multiple values comes from another datatable:

{ "YMDH": "2009-03-07 00:00:00.000", "SELL_DURATION": 222.999995 }, { "YMDH": "2009-03-07 01:00:00.000", "SELL_DURATION": 75.816664 }, { "YMDH": "2009-03-07 02:00:00.000", "SELL_DURATION": 39.349995 }, { "YMDH": "2009-03-07 03:00:00.000", "SELL_DURATION": 75.816664 }, { "YMDH": "2009-03-07 04:00:00.000", "SELL_DURATION": 39.349995 }, { "YMDH": "2009-03-07 05:00:00.000", "SELL_DURATION": 275.91666 }


Since first table will have only one records and another table will be having multiple records for that one row. how can i combine them both in my controller class to build proper JSON like this:

Desired Output:

var nwCustomers = [{ "ID": 1, "SHORT_NAME": "A", "CARRIER_NAME": "Carrier A", "SellDuration": [{ "YMDH": "2009-03-07 00:00:00.000", "SELL_DURATION": 222.999995 }, { "YMDH": "2009-03-07 01:00:00.000", "SELL_DURATION": 75.816664 }, { "YMDH": "2009-03-07 02:00:00.000", "SELL_DURATION": 39.349995 }, { "YMDH": "2009-03-07 03:00:00.000", "SELL_DURATION": 75.816664 }, { "YMDH": "2009-03-07 04:00:00.000", "SELL_DURATION": 39.349995 }, { "YMDH": "2009-03-07 05:00:00.000", "SELL_DURATION": 275.91666 }] }
];

I'm tring to do something like this but not able to get the desired output:
Expand|Select|Wrap|Line Numbers
  1. public class GridModel
  2. {
  3.     public DateTime YMDH { get; set; }
  4.     public double ID { get; set; }
  5.     public string SHORT_NAME { get; set; }
  6.     public string CARRIER_NAME { get; set; }
  7.     public double SELL_DURATION { get; set; }      
  8.     public GridSparklineModel SellDuration { get; set; }
  9. }
  10.  
  11.  
  12. public class GridSparklineModel
  13. {
  14.     public DateTime YMDH { get; set; }
  15.     public double SELL_DURATION { get; set; }       
  16. }
  17.  
Controller:

Expand|Select|Wrap|Line Numbers
  1.  public ActionResult FetchGraphDataJSON()
  2.         {
  3.             Grid grid = new Grid();
  4.             DataSet ds = grid.GetHistoryData();
  5.  
  6.             DataTable dt = ds.Tables[0];
  7.  
  8.             List<GridModel> data = new List<GridModel>();
  9.  
  10.             if (dt.Rows.Count != 0)
  11.             {
  12.                 StringBuilder sb = new StringBuilder();                
  13.  
  14.                 foreach (DataRow row in dt.Rows)
  15.                 {
  16.                     sb.Append(new GridModel { ID = Convert.ToDouble(@row["ID"].ToString()), SHORT_NAME = @row["SHORT_NAME"].ToString() });
  17.  
  18.                     double carrierId = Convert.ToDouble(@row["ID"].ToString());
  19.  
  20.                     DataRow[] rowsInOtherTable = ds.Tables[1].Select("ID = " + carrierId);
  21.  
  22.                     for(int i=0; i < rowsInOtherTable.Count(); i++)
  23.                     {
  24.                         // add with existing
  25.                     }
  26.  
  27.                     data.Add(new GridModel { ID = Convert.ToDouble(@row["ID"].ToString()), SHORT_NAME = @row["SHORT_NAME"].ToString(), CARRIER_NAME = @row["CARRIER_NAME"].ToString(), SellDuration = new GridSparklineModel { YMDH = DateTime.Parse(@rowsInOtherTable[0]["YMDH"].ToString()), SELL_DURATION = Convert.ToDouble(@rowsInOtherTable[0]["SELL_DURATION"].ToString()) } });
  28.                 }
  29.             }
  30.  
  31.             return Json(data, JsonRequestBehavior.AllowGet);
  32.         }
  33.  
  34.  
This is how m two datatables look:

First datatable has id values
ID SHORT_NAME CARRIER_NAME
1 A Carrier A
2 B Carrier B
3 C Carrier C
4 D Carrier D
5 E Carrier E
6 F Carrier F
7 G Carrier G


Based on above id values rows are filtered from this datatable 2 and added to make row of JSON

YMDH ID SELL_DURATION
2009-03-07 00:00:00.000 1 222.999995
2009-03-07 01:00:00.000 1 75.816664
2009-03-07 02:00:00.000 1 39.349995
2009-03-07 03:00:00.000 1 275.91666
2009-03-07 04:00:00.000 1 352.666641
2009-03-07 00:00:00.000 2 80.783324
2009-03-07 01:00:00.000 2 162.049985
2009-03-07 02:00:00.000 2 107.199989
2009-03-07 03:00:00.000 2 44.849994
2009-03-07 04:00:00.000 2 156.516658
2009-03-07 05:00:00.000 2 467.583312
2009-03-07 06:00:00.000 2 455.199977
and so on..
Mar 12 '14 #1
0 1148

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: randy | last post by:
Hello all, I have a DataTable which I am building column by column and adding rows after each new column. The DataTable columns match the columns in my database table. I'm building the...
4
by: Brian Wilson | last post by:
Hi Newsgroup! I know many of you must have bought the Access Developer's Handbook and used the Tagged Values class, so perhaps I can ask someone to check something. On page 303, it describes the...
1
by: Mike | last post by:
I have an ASP.NET/VB app that updates values in a DataTable over the course of about 3 different pages. On the way out of the first of these pages, I explicitly build the DataTable from values in...
1
by: Paul Perot | last post by:
Hi All: I have a DataTable that I have defined Globally. I populate this datatable dynamically with file/folder information that I read directly from the server. I use this datatable...
1
by: Janaka | last post by:
I've got a DataSet which I'd like to be able to get all the values out of one of the DataTable columns. These may be either string or integer values. What I'd like ideally is to be able to get...
4
by: bijoy | last post by:
My page has a Listbox called Rooms, a text field called chairPerson and a button called Add. When Add is clicked, the script needs to display the selected info in a datagrid. For example, if...
2
by: Joe | last post by:
Hi I am building a datatable manually and putting in a grid I would like to edit a row then determine that the table has changes and needs to be saved The problem I am having is that all...
8
by: MD | last post by:
I want to put the xml document into the JSON like the following. However, usually my xml document has new line for the xml document. Is there any option to skip the new line in the JSON or I...
3
by: John | last post by:
Hi Sorry, I am new to this. Is there a way to cerate a datatable from a OleDbDataReader? Thanks Regards
1
by: kirankumarn | last post by:
i want to read DataTable values in javascript & want to bind listbox using DataSource & DataBind is it possible in javascript because i want to use ajax concepts for my project
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.