The development of a Magento 2 extension necessitates customization to meet organizational needs.
Developers may require adjustment while creating an extension so that the controller returns data in the JSON format.
The controller is called after the AJAX request has been made. When we call a certain controller, we frequently need to return data. If we return the data in JSON format in these circumstances, it would be a practical choice, especially if we needed to return several values.
For instance, all the states of the chosen country are shown when a consumer chooses that country.
This article provides a programmatic method to return JSON response from a controller Magento 2.
Visit an article, https://meetanshi.com/blog/return-json-response-from-controller-in-magento-2/