---
title: "How to implement response type JSON in .NET Core API?"  
description: "How to implement response type JSON in .NET Core API?"  
author: "Rocky Dada"  
published: 2023-08-30  
updated: 2023-09-02  
canonical: https://www.mindstick.com/forum/159758/how-to-implement-response-type-json-in-dot-net-core-api  
category: "web api"  
tags: [".net core", ".net core api"]  
reading_time: 2 minutes  

---

# How to implement response type JSON in .NET Core API?

[Explain the concept](https://www.mindstick.com/forum/159605/explain-the-concept-of-unique-key-violation-error) of [response](https://www.mindstick.com/forum/12719/how-to-make-response-write-display-special-character-like-lt-gt) type [JSON](https://www.mindstick.com/forum/34446/convert-json-string-to-object) in the [context](https://www.mindstick.com/forum/23245/what-is-context-in-android) of .NET Core.

## Replies

### Reply by Aryan Kumar

To implement response type JSON in a .NET Core API, you can use the `JsonResult` class. The `JsonResult` class is a type of `ActionResult` that can be used to return JSON data from an API endpoint.

To use the `JsonResult` class, you need to first install the `Microsoft.AspNetCore.Mvc.NewtonsoftJson` NuGet package. This package provides the Newtonsoft.Json library, which is used to serialize and deserialize JSON data.

Once you have installed the NuGet package, you can use the `JsonResult` class in your API endpoints. For example, the following code returns a JSON object with a single property named "message":

C#

```plaintext
public class MessageController : Controller
{
  [HttpGet("/message")]
  public JsonResult GetMessage()
  {
    return Json(new { message = "Hello, world!" });
  }
}
```

In this example, the `GetMessage()` action returns a `JsonResult` object with a single property named "message". The value of the "message" property is a string literal that contains the text "Hello, world!".

When the `GetMessage()` action is called, the Newtonsoft.Json library will serialize the JSON object and write it to the response stream. The response content type will be set to `application/json`.

You can also use the `JsonResult` class to return JSON objects that contain complex data structures. For example, the following code returns a JSON object that contains an array of numbers:

C#

```plaintext
public class NumberController : Controller
{
  [HttpGet("/numbers")]
  public JsonResult GetNumbers()
  {
    return Json(new[] { 1, 2, 3, 4, 5 });
  }
}
```

In this example, the `GetNumbers()` action returns a `JsonResult` object with an array of numbers. The array contains the numbers 1, 2, 3, 4, and 5.

The `JsonResult` class is a powerful tool that can be used to return JSON data from .NET Core APIs. By using the `JsonResult` class, you can easily and efficiently return JSON data from your APIs.


---

Original Source: https://www.mindstick.com/forum/159758/how-to-implement-response-type-json-in-dot-net-core-api

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
