How To

How to Return JSON Data from Controller While calling AJAX in Magento 2

Technically, API stands for Application Programming Interface. At some point or another, large companies need to build APIs to serve easy access to their customers. To integrate third-party tool access, for such purpose JSON comes to the rescue. Basically, JSON means JavaScript Object Notation, which is a lightweight data-interchange format supported by Magento. Instead of using standard XML response, JSON helps to increase the productivity of the web applications by Minimizing the number of requests and amount of data transmitted between client and server.

Recently working on a custom extension development, we came across the requirement of using third-party API integration along with AJAX to save time and efforts of consumers. Keeping client’s requirement in mind and going through docs of API, we finally decided to use JSON response to reduce response time and data transmitted between extension and API server. To increase the efficiency of extension we have added a topping of AJAX to display data on the same page. Presenting guide to returning JSON data from controller While calling ajax in Magento 2.

By default, Magento has served a list of TYPE_JSON constant variables in ResultFactory. Magento\Framework\Controller\ResultFactory
Among which, here we have used json to fulfill the purpose but you can use any constant variable as per your requirement.
1. const TYPE_JSON = ‘json’
2. const TYPE_RAW = ‘raw’
3. const TYPE_REDIRECT = ‘redirect’
4. const TYPE_FORWARD = ‘forward’
5. const TYPE_LAYOUT = ‘layout’
6. const TYPE_PAGE = ‘page’

Firstly, create ‘Jsonrequest.php’ file in at below path using following code.
app\code\Vendor\Extension\controller\Index\Jsonrequest.php
Because $this->resultFactory object is already defined in Magento\Framework\App\Action\Context we don’t need to create another.


Congratulations! You are done. You have successfully returned json data from controller. You can use or modify this code according to your convenience and requirement.

Lastly, comment down below if you are looking for any help regarding this code and tell us how you have used this code. Stay tuned for more.
Happy Coding!

Click to rate this post!
[Total: 12 Average: 4.2]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

12 hours ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

3 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

3 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

4 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

6 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We have brought exciting news for Magento store owners. Hyvä Themes recently released 1.3.6 and…

6 days ago