Sort Json by 2 keys.

function sortJSONBy2Keys(a,b,c){var d=function(b,c){return b>c?1:b<c?-1:0};return a.sort(function(a,e){var f=a[b],g=e[b],h=a[c],i=e[c];return d([d(f,g),d(h,i)],[d(g,f),d(i,h)])})}

var sortedJson = sortJSONBy2Keys(originalJson,'key1','key2');




Experiment with JSON date format.

(by AXDS)

I have found out that the JSON standard for date is in this format: ‘yyyy-MM-ddTHH:mm:ssZ’. This format is the preferred way to handle dates inside a JSON object.

For example, 21st March 2016 at 17:04:33, would be, 2016-03-21T17:04:33.000Z.

In order to convert a JavaScript Date into the JSON standard format, toJSON() can be called on this date. toISOString() is another function that can be used to convert a Date into this format.

There is no real functional difference between these functions, in fact, toJSON() will call toISOString(). The main difference is that toJSON() is included in JS 1.8.5, while toISOString() is included in JS 1.8. This makes toJSON() preferable for converting Date to JSON’s standard date format.

This JSON date format can be parsed into milliseconds using the usual Date.parse() function.


SQL Server Table to JSON Object Array

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Web.Script.Serialization;
using System.IO;
using System.Text;
using System.Web.Services;
using System.Text.RegularExpressions;
using System.Collections;

private static string databaseTableToJsonArrayObject()
string CONNECTIONSTRING_KEY_NAME = "Your_ConnectionStrings_Name_Attribute";
string TABLE_TO_CONNECT_TO = "Your_Tables_Name";
string SELECT = "DISTINCT *"; //string SELECT = "SELECT id"
string WHERE = ""; //string WHERE = "WHERE id = 1"
string ORDERBY = ""; //string ORDERBY = "ORDER BY id ASC|DESC"

DataTable dt = new DataTable();
using (SqlConnection conString = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTIONSTRING_KEY_NAME].ConnectionString))

using (SqlCommand sqlCmd = new SqlCommand("SELECT " + SELECT + " FROM " + TABLE_TO_CONNECT_TO + WHERE + ORDERBY, conString))
SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dt.Rows)
row = new Dictionary<string, object>();

int columnCount = 0;
foreach (DataColumn col in dt.Columns)
row.Add(columnCount.ToString(), dr[col]);
return serializer.Serialize(rows);



AXDS Plugin: JSON to styled jQuery Datatable

(by AXDS)

Useful for quick data visualisation. Converts a JSON object into a styled filterable/sortable table. Also uses Bootstrap and jQuery Datatable libraries.


The plugin, once called on a target location, requires a table ID and an object of data in order to work.


<div id="examplePlace"></div>


var tableId = "tableID";
var targetElement = "#examplePlace";
"tableId": tableId,
"data": {
"tableColumnHeaders": tableColumnHeaders,
"tableContentsValues": tableContentsValues
"options": tableOptions

The data and objects are formatted as below:

The tableData is split into the header and the values to populate the rows. The id inside the headers is linked to the key of the values. For example: The column ‘fstName’ will be populated with any keys with ‘fstName’.

 var tableColumnHeaders = [{
"id": "fstName",
"text": "First Name"
}, {
"id": "lstName",
"text": "Last Name"
}, {
"id": "mobile",
"text": "Mobile Number"
}, {
"id": "dob",
"text": "Date of Birth"

var tableContentsValues = [{
"fstName": "Wade",
"lstName": "Wilson",
"mobile": "07567 567 432",
"dob": "01/01/1985",
}, {
"fstName": "Peter",
"lstName": "Parker",
"mobile": "07594 567 890",
"dob": "01/01/1995",
}, {
"fstName": "Fred",
"lstName": "Flintstone",
"dob": "01/01/1965",
}, {
"fstName": "Tony",
"lstName": "Stark",
"mobile": "01345 987 234",
"dob": "01/01/1975",

The tableOptions is for styling the table. This is pretty self explanatory.

var tableOptions = [{
"tableBordered": true,
"tableStriped": true,
"responsive": true,
"cellspacing": 0,
"noWrap": true,
"width": "100%",
"rowStyling": {
"fontStyle": "italic",
"color": "red"
"tableStyling": {
"textAlign": "center",
"color": "blue"

This will produce a styled table with the given ID in the target location. This table and ID can be used with the jQuery Datatable library to produce a filterable/sortable table.