The JSON libname engine is the recommended method for reading JSON data in SAS. JSON is also increasingly used as a standard for data exchange among widely used systems (such as FHIR for health data). In this article, we learned about different methods to convert DataTable to JSON and investigated how these methods perform on example data of different sizes.The number of SAS users who need to access JSON data has skyrocketed, thanks mainly to the proliferation of REST-based APIs and web services. LINQ method shows better timings as the size of the data increases but it uses the most memory in all examples.Īnd in the end, a bit of a mediocre performance of the method puts it in last place. The speed drops as the data size increases, but the memory usage is even better than we get with JSON.NET. Based on this, we can conclude it is the best choice for this task.Ī bit surprisingly, the performance we get from the StringBuilder method is only slightly worse. | StringBuilder | 915.7 ms | 4.27 ms | 3.57 ms | 914.9 ms | 4 | 238.14 MB |Īs we can see, besides simplicity, JSON.NET also has the best performance, both in terms of speed and memory usage, in all examples. | SystemTextJson | 82.51 ms | 1.227 ms | 1.088 ms | 82.92 ms | 4 | 50.81 MB |Īnd finally, let’s see the performance on 1,000,000 records: | Method | Mean | Error | StdDev | Median | Rank | Allocated | What about 100,000 records: | Method | Mean | Error | StdDev | Median | Rank | Allocated | Let’s now see how the methods perform on a DataTable with 10,000 records: | Method | Mean | Error | StdDev | Median | Rank | Allocated | NET/C# content and get paid? > JOIN US! << | Method | Mean | Error | StdDev | Median | Rank | Allocated | Wanna join Code Maze Team, help us produce more awesome. Public class Benchmark_1000000 : BenchmarkĭataTable = GenerateDummyDataTable(1000000) įirst, let’s see the performance on a DataTable with 1,000 records: Public class Benchmark_100000 : BenchmarkĭataTable = GenerateDummyDataTable(100000) Since we want to test our methods against a DataTable of different sizes, we will define additional classes that inherit the main Benchmark class but generate test data of different sizes: public class Benchmark_10000 : BenchmarkĭataTable = GenerateDummyDataTable(10000) Public void StringBuilder() => Methods.DataTableStringBuilder(dataTable) Public void Linq() => Methods.DataTableLinq(dataTable) Public void SystemTextJson() => Methods.DataTableSystemTextJson(dataTable) Public void NewtonsoftJsonNet() => Methods.DataTableNewtonsoftJsonNet(dataTable) Now we can create the methods we will use to perform benchmarking: Var jsonStringBuilder = new StringBuilder() The first one uses StringBuilder: public static string DataTableStringBuilder(DataTable dataTable) Let’s examine a more “manual” approach to this problem that revolves around the idea of composing a JSON string on our own.įor this, we will define two methods. Now we can use JsonSerializer on this new structure to get the resulting JSON string. Return .Serialize(data) Īlthough DataTable serialization is not supported, with a bit of the LINQ magic, we can convert DataTable to a list of KeyValue objects. ToDictionary(col => col.ColumnName, c => row)) We can still make use of it with a small workaround: public static string DataTableSystemTextJson(DataTable dataTable) Unlike JSON.NET, the native library does not support DataTable conversion out-of-the-box. We can see it is pretty straightforward to convert DataTable to JSON using the JsonConvert class which is defined in the Newtonsoft.Json namespace. So, we can implement a method that takes advantage of this: public static string DataTableNewtonsoftJsonNet(DataTable dataTable)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |