How to get response in JSON from Gemini API

If you are creating an AI agent and any SaaS tool, and want to use an AI LLM model like Gemini to make your product very powerful, then this article is for you.

When you are working with an AI model then you need to understand prompt engineering in si…


This content originally appeared on DEV Community and was authored by Akhil Srivastava

If you are creating an AI agent and any SaaS tool, and want to use an AI LLM model like Gemini to make your product very powerful, then this article is for you.

When you are working with an AI model then you need to understand prompt engineering in simple word prompt engineering is giving a command to AI model to understand the requirement and give you the extact solution. Now if you are working with AI then you have to explain AI in the best possible way so that it may give you the solution accodingly.

It very easy right

Like if you want that Gemini return the solution in JSON then you have to type

Give me the top 10 news in new york, USA. Format the response and return in JSON

So her basically you are giving command then give the top 10 news in JSON. then Gemini will do this for you.

Image description

Here is the image you can check.

and If you are working with node js then you have to use one package name that is

npm i @google/generative-ai

This is an official package by google in node js

You can create function like

const generateAiResponse = () => {
  if (!process.env.GEMINI_AI_API_KEY) {
    console.error('GENERATIVE_AI_API_KEY is not defined');
    return;
  }
const genAI = new GoogleGenerativeAI(process.env.GEMINI_AI_API_KEY);
// add the model of gemini which you wan to use
  const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
  const result = await model.generateContent(prompt);

  const responseText = await result.response.text();
  console.log("Parsed JSON Response:", responseText);
  return responseText;
}

Hope this article helped you in your research if you really liked it then click on the like button.

For hiring me as your developer you can connect me on gmail: luvprogramming@gmail.com


This content originally appeared on DEV Community and was authored by Akhil Srivastava


Print Share Comment Cite Upload Translate Updates
APA

Akhil Srivastava | Sciencx (2025-03-01T16:46:20+00:00) How to get response in JSON from Gemini API. Retrieved from https://www.scien.cx/2025/03/01/how-to-get-response-in-json-from-gemini-api/

MLA
" » How to get response in JSON from Gemini API." Akhil Srivastava | Sciencx - Saturday March 1, 2025, https://www.scien.cx/2025/03/01/how-to-get-response-in-json-from-gemini-api/
HARVARD
Akhil Srivastava | Sciencx Saturday March 1, 2025 » How to get response in JSON from Gemini API., viewed ,<https://www.scien.cx/2025/03/01/how-to-get-response-in-json-from-gemini-api/>
VANCOUVER
Akhil Srivastava | Sciencx - » How to get response in JSON from Gemini API. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/01/how-to-get-response-in-json-from-gemini-api/
CHICAGO
" » How to get response in JSON from Gemini API." Akhil Srivastava | Sciencx - Accessed . https://www.scien.cx/2025/03/01/how-to-get-response-in-json-from-gemini-api/
IEEE
" » How to get response in JSON from Gemini API." Akhil Srivastava | Sciencx [Online]. Available: https://www.scien.cx/2025/03/01/how-to-get-response-in-json-from-gemini-api/. [Accessed: ]
rf:citation
» How to get response in JSON from Gemini API | Akhil Srivastava | Sciencx | https://www.scien.cx/2025/03/01/how-to-get-response-in-json-from-gemini-api/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.