How to Query JSON in C# using SQL, LINQ, ADO.NET
Tags: webdev, devops, csharp, tutorial
Published: 2023-11-08 14:56:42

---
canonical_url: https://developer.mescius.com/blogs/how-to-query-json-in-c-using-sql-linq-adonet
description: Learn how to query JSON data in C# using SQL, LINQ, ADO.NET. See more from ComponentOne today.
---
In today’s connected world, data is piling up very fast. We are generating more data than ever in human history, and a lot of this data is getting stored in non-relational formats like JSON documents. JSON has become a ubiquitous format for modern-day applications - being used for config files, data transfer between APIs, client & server, data storage, etc. Given its simplicity, working with JSON data cannot be anything but simple.

In this blog, we will look at [ComponentOne DataConnector for JSON](https://developer.mescius.com/componentone/net-data-service-components/data-connectors "https://developer.mescius.com/componentone/net-data-service-components/data-connectors"), which offers an effortless way to connect to JSON data using ADO.NET or Entity Framework Core. We'll discuss using the C1 ADO. NET provider for JSON to query JSON data using SQL and LINQ.

### How to Query JSON Using SQL

In this process, we will be following the same approach that we generally use while working with databases, i.e.:

1.  [Creating a Connection String](#Connection-String)
2.  [Creating a Connection Object](#Connection-Object)
3.  [Querying data using SQL](#Querying-Data)

But first, let’s start by installing the required NuGet package.

*   Open the Project menu and select **Manage NuGet Packages.**
*   Browse for **‘C1\. AdoNet. Json’** NuGet package and click install.

### <a id="Connection-String"></a>Creating a Connection String

In order to connect with our data source, we would need a connection string. We will be using [**C1JsonConnectionStringBuilder**](https://developer.mescius.com/componentone/docs/services/online-dataconnector/C1.AdoNet.Json~C1.AdoNet.Json.C1JsonConnectionStringBuilder.html) to create our connection string. Creating a connection string requires three items:

1.  **JSON DataModel**: It specifies the access mechanism that is used for reading the JSON data. Specifying the DataModel, let’s you configure how object arrays are modeled as tables. The different data models available are – the Top Level Document Model, Flattened Model, and Relational Model. You can read more about them [**here**](https://developer.mescius.com/componentone/docs/services/online-dataconnector/jsondatamodels.html).
2.  **Uri**: Specifies the URI for the JSON resource location. You can connect to local files or HTTP streams.
3.  **JSON Path**: Specifies which node to read from the JSON content

The following shows an example of creating a connection string for flat data:

```
    public static class Utils
    {
            public static string JsonDataPath => "Data\\EmployeesData.json";
            public static C1JsonConnectionStringBuilder JsonConnectionBuilder { get; }

            static Utils()
            {
                JsonConnectionBuilder = new C1JsonConnectionStringBuilder()
                {
                    DataModel = "Document",
                    Uri = JsonDataPath,
                    JsonPath = "$.employees"
                };
            }
    }
```

Similarly, the following shows creating a connection string for relational data:

```
    public static class Utils
    {
            public static string JsonCustomerDataPath => "Data\\CustomersData.json";
            public static C1JsonConnectionStringBuilder CustomerDataConnectionStringBuilder { get; }

            static Utils()
            {
                CustomerDataConnectionStringBuilder = new C1JsonConnectionStringBuilder()
                {
                    DataModel = "Relational",
                    Uri = JsonCustomerDataPath,
                    JsonPath = "$.customers;$.customers.Transactions"
                };
            }
    }
```

To learn more about the connection string format, please refer [**here**](https://developer.mescius.com/componentone/docs/services/online-dataconnector/jsonconnection.html).

### <a id="Connection-Object"></a>How to Create a Connection Object

Now, we’ve got our connection string, so the next step is to initialize a connection instance using [**C1JsonConnection**](https://developer.mescius.com/componentone/docs/services/online-dataconnector/C1.AdoNet.Json~C1.AdoNet.Json.C1JsonConnection.html) class as follows:

```
    private void CreateJsonConnection()
    {
    _jsonConnection = new C1JsonConnection(_connBuilder.ConnectionString);
    }
```

### <a id="Querying-Data"></a>Querying Data Using SQL

Now that the connection is ready, we can query data using SQL by creating an object of the [**C1JsonDataAdapter**](https://developer.mescius.com/componentone/docs/services/online-dataconnector/C1.AdoNet.Json~C1.AdoNet.Json.C1JsonDataAdapter.html) as follows:

```
    C1JsonDataAdapter adapter = new C1JsonDataAdapter(_jsonConnection, sql);
    var table = new DataTable();
    adapter.Fill(table);
```

The following shows a sample flat data and the corresponding query for the same:

![Query JSON in C# using SQL, LINQ, ADO.NET](//cdn.mescius.io/umb/media/szhl3n0g/image2_flat-data.png)

```
    C1JsonDataAdapter adapter = new C1JsonDataAdapter(_jsonConnection, “select * from employees”);
    var table = new DataTable();
    adapter.Fill(table);
```

With relational data it may look something like this:

![Query JSON in C# using SQL, LINQ, ADO.NET](//cdn.mescius.io/umb/media/o2amlxe5/image3_relational.png)

```
    C1JsonDataAdapter adapter = new C1JsonDataAdapter(_jsonConnection, (“Select FirstName, LastName, Amount from customers INNER JOIN Transactions ON customers._id = Transactions.customers_id where Transactions.IsSuccess=false”);
    var table = new DataTable();
    adapter.Fill(table);
```

Once the DataTable gets populated, the data can be displayed in a grid as shown below:

![Query JSON in C# using SQL, LINQ, ADO.NET](//cdn.mescius.io/umb/media/wvgnr3om/image4-3.png)

### Querying JSON using LINQ in Entity Framework Core

In this section, we’ll discuss how we can query JSON data using LINQ in Entity Framework Core. Let’s begin by installing the required NuGet package i.e. **C1.EntityFrameworkCore.Json**

Once the NuGet package is installed, next we need to set the DbContext and provide the connection string to the JSON data source. We’ll use **C1JsonConnectionStringBuilder** for this purpose, as shown below:

```
    public abstract partial class DocumentContext : DbContext
    {
            private C1JsonConnectionStringBuilder _builder;

            public DocumentContext(C1JsonConnectionStringBuilder builder)
            {
                _builder = builder;
                Database.AutoTransactionsEnabled = false;
            }

            public DocumentContext(DbContextOptions<DocumentContext> options)
                : base(options)
            {
                Database.AutoTransactionsEnabled = false;
            }

            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                if (!optionsBuilder.IsConfigured)
                {
                    optionsBuilder.UseJson(_builder.ConnectionString);
                }
            }
    }
```

### How to Create the DbContext

The above **DocumentContext** class can serve as the base class for DbContext of our JSON documents. The model entities can be defined in the child classes.

The following shows an example of DbContext for a flat JSON data source:

```
    public class EmployeesContext : DocumentContext
    {
            public virtual DbSet<Employee> Employees { get; set; }

            public EmployeesContext() : base(Utils.JsonConnectionBuilder) { }

            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Employee>(entity =>
                {
                    entity.ToTable("employees");
                    entity.HasKey(x => x.Id);
                    entity.Property(e => e.Id).HasColumnName("Id");
                    entity.Property(e => e.FirstName).HasColumnName("FirstName");
                    entity.Property(e => e.LastName).HasColumnName("LastName");
                    entity.Property(e => e.Email).HasColumnName("Email");
                    entity.Property(e => e.DOB).HasColumnName("DOB");
                    entity.Property(e => e.Address).HasColumnName("Address");
                    entity.Property(e => e.State).HasColumnName("State");
                    entity.Property(e => e.Company).HasColumnName("Company");
                    entity.Property(e => e.Gender).HasColumnName("Gender");
                    entity.Property(e => e.JobTitle).HasColumnName("JobTitle");
                    entity.Property(e => e.Skill).HasColumnName("Skill");
                    entity.Property(e => e.Salary).HasColumnName("Salary");
                });
            }
    }
```

The following shows an example of DbContext for a relational JSON data source:

```
    public class CustomersContext : DocumentContext
    {
            public virtual DbSet<Customer> Customers { get; set; }
            public virtual DbSet<Transaction> Transactions { get; set; }

            public CustomersContext() : base(Utils.CustomerDataConnectionStringBuilder) { }

            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Transaction>(entity =>
                {
                    entity.ToTable("Transactions");
                    entity.HasOne(e => e.Customer).WithMany(x => x.Transactions).HasForeignKey("customers_id");
                    entity.Property(e => e.Id).HasColumnName("_id");
                    entity.Property(e => e.Amount).HasColumnName("Amount");
                    entity.Property(e => e.Credited).HasColumnName("Credited");
                    entity.Property(e => e.IsSuccess).HasColumnName("IsSuccess");
                    entity.Property(e => e.Date).HasColumnName("Date");
                });

                modelBuilder.Entity<Customer>(entity =>
                {
                    entity.ToTable("customers");
                    entity.Property(e => e.Id).HasColumnName("_id");
                    entity.Property(e => e.FirstName).HasColumnName("FirstName");
                    entity.Property(e => e.LastName).HasColumnName("LastName");
                    entity.Property(e => e.DOB).HasColumnName("DOB");
                    entity.Property(e => e.State).HasColumnName("State");
                    entity.Property(e => e.Gender).HasColumnName("Gender");
                });
            }
    }
```

With the ComponentOne DataConnectors scaffolding feature, these classes can be generated automatically. It’s generated by running the **Scaffold-DbContext** command. See the [scaffolding topic in our documentation](https://developer.mescius.com/componentone/docs/services/online-dataconnector/scaffolding.html "https://developer.mescius.com/componentone/docs/services/online-dataconnector/scaffolding.html") for details.

### Querying the DbContext Using LINQ

Now that the DbContext are set, the JSON datasource can be queried using LINQ. We can use both Query syntax or the Method syntax:

```
    _context = new EmployeesContext();
    var employees = await _context.Employees.ToListAsync();

    // Employee count of companies starting with 'A'
    var employeeCountData = from employee in employees
                            where employee.Company.StartsWith("A", StringComparison.OrdinalIgnoreCase)
                            group employee by employee.Company into grp
                            select new { Company = grp.Key, NumberOfEmployees = grp.Count() };

    // Salary growth of people working in Dynabox
    var salaryGrowthData = employees
             .Where(x => x.Company == "Dynabox").OrderBy(x => x.DOB)
             .Select(x => new { Age = DateTime.Now.Year - x.DOB.Year, Salary = x.Salary })
             .Where(x => x.Age >= 18)
             .GroupBy(x => x.Age)
             .Select(x => new { Age = x.Key, AverageSalary = x.Select(o => o.Salary).Average() });
```

The data can then be used in a grid/chart as shown below:

![Query JSON in C# using SQL, LINQ, ADO.NET](//cdn.mescius.io/umb/media/m5mklafx/image6-3.png)

### Conclusion

In this blog, we saw querying flat/relational JSON data using the C1 ADO. NET provider for JSON. While this blog shows the basic usage of the ADO. NET provider for JSON, this library also provides advanced features like as [**Authorization**](https://developer.mescius.com/componentone/docs/services/online-dataconnector/jsonauth.html), [**Caching**](https://developer.mescius.com/componentone/docs/services/online-dataconnector/jsoncaching.html), [**Connection Pooling**](https://developer.mescius.com/componentone/docs/services/online-dataconnector/jsonconnectionpooling.html), etc. Read more from the product documentation [here](https://developer.mescius.com/componentone/docs/services/online-dataconnector/jsongettingstarted.html).

Please feel free to ask if you have any questions.

[Read Documentation](https://developer.mescius.com/componentone/docs/services/online-dataconnector/jsongettingstarted.html) | [Download Sample](https://github.com/GrapeCity/ComponentOne-Service-Components-Samples/tree/master/DataConnector/NetCore/Json)