Order of performing sorting on a big JSON object

380 views Asked by At

i have a big json object with a list of "tickets". schema looks like below

{ "Artist": "Artist1", "Tickets": [ { "Id": 1, "Attr2Array": [ { "Att41": 1, "Att42": "A", "Att43": null }, { "Att41": 1, "Att42": "A", "Att43": null }, ], . . . (more properties) "Price": "20", "Description": "I m a ticket" }, { "Id": 4, "Attr2Array": [ { "Att41": 1, "Att42": "A", "Att43": null }, { "Att41": 1, "Att42": "A", "Att43": null }, ], . . . . (more properties) "Price": "30", "Description": "I m a ticket" } ] }

each item in the list has around 25-30 properties (some simple types, and others complex array as nested objects)

i have to read the object from an api endpoint and extract only "ID" and "Description" but they need to be sorted by "Price" which is an int for example

In what order shall i proceed with this data manipulation

  1. Shall i use the json object, deserialised it into another object with just those 2 properties (which i need) and THEN perform sort "asc" on the "Price"?

Please note that after i have the sorted list i will have to convert it back to a json list because the front end consumes a json after all.

What i dont like about this approach is the cycle of serialisation and deserialisation that happens

or

  1. I perform a sort on the json object first (using for example a binary/bubble sort) and then use the object to create a strongly typed (deserialised) object with just those 2 properties and then serialise it back to pass to the front end

I dont know how performant the bubble sort will be and if at all i will get any gain in performance for large chunks of data processing.

I also need to keep in mind that this implementation can take into account other properties like "availabilitydate" because at a later date, this front end could add one more filter like "availabilitdate" asc

any help is much appreciated

thanks

1

There are 1 answers

0
Denis Voituron On

You can deserialize your JSON string (or file) using the Microsoft System.Web.Extensions and JavaScriptSerializer.DeserializeObject.

First, you must have classes associated to your JSON. To create classes, select your JSON sample data and, in Visual Studio, go to Edit / Paste Special / Paste JSON As Classes.

Next, use this sample to deserialize a JSON string to typed objects, and to sort all Tickets by Price property using Linq.

String json = System.IO.File.ReadAllText(@"C:\Data.json");
var root = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Rootobject>(json);
var sortedTickets = root.Tickets.OrderBy(t => t.Price);