📒
Notes
Cloud ComputingData Science/AIGame Development
  • Home
  • Big O
  • Data Structures & Algorithms
    • Data Structures
      • Array
      • Stack
      • Queue
      • Linked List
      • Binary Tree
    • Algorithms
      • Searching
      • Sorting
      • Graphs
        • Searching
        • Minimum Spanning Tree
        • Shortest Path Algorithms
      • String Algorithms
  • Object Oriented Programming
  • Languages
    • HTML/CSS
      • CSS
    • C++
    • C#
      • Types
      • Keywords
        • Modifiers
          • Access Modifiers
        • Method Parameters
      • Operators and Expressions
      • Collections
      • Constructors
      • Delegates
      • Indexers
      • Concepts
      • Features
        • LINQ
          • Operators
          • Working with Data
          • Methods
          • Resources
        • Asynchronous Programming
        • Reflection
    • Dart
    • GraphQL
    • JavaScript
      • Variable and Parameter
      • Built-in objects
        • Array
        • Built-in Functions
      • Functions
      • Classes
      • Prototype
      • Libraries
        • jQuery
        • React
          • Components
          • State and Lifecycle
          • Hooks
            • useState
            • useEffect
          • Resources
      • Testing Framework
      • Web APIs
    • Kotlin
      • Basics
    • Python
      • Basics
      • Data Structures
      • Functions
      • Resources
        • Flask
    • SQL
      • Basics
      • Operators
      • JOINs
      • Aggregations
      • Subqueries
      • Views
      • Functions
        • Window Functions
      • Stored Procedures
      • Performance Tuning
      • Extras
    • Resources
  • 🌐Web Frameworks
    • Angular
      • Templates
      • Directives
        • Attribute Directives
        • Structural Directives
    • ASP.NET
      • Fundamentals
        • Dependency Injection
        • Middleware
        • Session & State Management
      • Web apps
        • MVC
          • Controllers
            • Filters
          • Models
            • Model Binding
            • Model Validation
          • Views
            • Tag Helpers
            • View Components
          • Features
        • Client-side development
      • Web APIs
        • Controller-based APIs
        • Minimal APIs
        • OpenAPI
        • Content Negotiation
      • SignalR
      • Host and Deploy
        • IIS
      • Security
    • Django
      • The Request/Response Cycle
    • Terminologies
      • Web Server
        • Internet Information Services
    • Resources
  • 📱App Frameworks
    • Introduction
      • Resources
    • Xamarin
      • Lifecycle
      • Custom Renderers & Effects
      • Behaviors
      • Triggers
      • Gestures
      • Commands
      • Dependency Service in XF
      • Libraries
      • Showcase
    • .NET MAUI
      • Controls
      • Navigation
      • Storage Options
  • Multi-Platform Frameworks
    • .NET
      • .NET Framework
        • ADO.NET
        • WCF
      • Fundamentals
        • Logging
        • Testing
      • Advanced
        • Asynchronous Programming
        • Parallel Programming
        • Threading
        • Memory Management
          • Garbage Collection
    • Flutter
  • Object-Relational Mappers
    • Entity Framework
      • Application Models
      • Configuration
      • Setting Up
      • Advanced
  • Databases
    • Introduction
      • DBMS Architecture
      • Normalization
      • Database Transaction Models
    • Relational Databases
      • Microsoft SQL Server
        • Basics
        • Functions
        • Stored Procedures
        • Error Handling
        • Log Shipping
        • Querying and Manipulating JSON data
        • Statements
        • Topics
        • Extras
    • Non-Relational Databases
      • MongoDB
      • Redis
        • Data Structures
        • Introduction
        • Managing Database
  • Tools
    • Version Control
      • Git
        • Setup and Config
        • Basics
          • Sharing and Updating Projects
        • Resources
      • Perforce Helix
    • GitHub
    • Powershell
  • Software Development
    • Software Development Life Cycle
    • Software Design Patterns
      • GoF Design Patterns
      • Architectural Patterns
        • MVC
        • MVVM
        • N-tier Architecture
        • Onion Architecture
        • Data Transfer Objects
      • CQRS
    • Software Design Principles
      • S.O.L.I.D. Priniciple
  • System Design
    • Topics
      • Load Balancing
  • Topics
    • JWT
    • Caching
      • Static vs Dynamic Caching
    • OSI model
      • HTTP
    • Glossary
    • API
      • SOAP
      • REST
    • Microservices
    • WebHooks
    • Practice
    • Operating Systems
      • Windows
    • Architecture
  • 🔖Bookmarks
  • 🔗Resources
Powered by GitBook
On this page
  • Getting results of queries in JSON
  • Reading JSON in tabular form

Was this helpful?

  1. Databases
  2. Relational Databases
  3. Microsoft SQL Server

Querying and Manipulating JSON data

S.No.
State
Capital
RTO Code

1

Telangana

Hyderabad

TG

2

Karnataka

Bengaluru

KA

3

Kerala

Thiruvananthapuram

KR

4

Madhya Pradesh

Bhopal

MP

5

Maharashtra

Mumbai

MH

6

Gujarat

Gandhi Nagar

GJ

Getting results of queries in JSON

Adding the below line at the end of the query will return the result in JSON format. FOR JSON PATH

We can also have a root element by adding ROOT('root_name') after FOR JSON PATH

SELECT * FROM CapitalOfIndianStates
FOR JSON PATH, ROOT('StateCapitals')

Using this SQL statement will help us directly returning JSON data at the database level without the need of serializing the data at the application level.

Reading JSON in tabular form

OPENJSON()

This function is used to parse the entire JSON object in tabular form. Let's understand with an example.

Consider we have a JSON object which we have saved into a variable.

DECLARE @Info NVARCHAR(MAX);
SET @Info = N'[
    {
        "Id": 1,
        "FirstName": "John", 
        "LastName": "Doe",
        "Gender": 'M',
        "Age": 22        
    },
    {
        "Id": 2,
        "FirstName": "Jane", 
        "LastName": "Doe",
        "Gender": 'F',
        "Age": 22        
    },
]';

To display these JSON object in tabular form (rows/columns) we pass this variable as an argument to OPENJSON(). After that, we need to tell the function what datatype will go with each property. We do this by specifying each column name with its corresponding data type inside theWITHclause.

SELECT * FROM OPENJSON(@Info)
WITH (
    Id INT 'strict S.Id'
    FirstName VARCHAR(50) '$.FirstName',
    LastName VARCHAR(50) '$.LastName',
    Gender CHAR(2) '$.Gender',
    Age INT '$.Age'
);

Note: strict keyword we used in our code is to speecify that Id must exist in the JSON object.

PreviousLog ShippingNextStatements

Last updated 2 years ago

Was this helpful?

T-SQL JSON: Query and Manipulate JSON Data with T-SQL | Pluralsight
Logo