> For the complete documentation index, see [llms.txt](https://nosicode.gitbook.io/faq/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nosicode.gitbook.io/faq/intermediate/how-to-consume-a-json.md).

# How to consume a JSON... GET and POST

## &#x48;***ow to consume a JSON and populates the data in a table***

Using the method: Core.httpGet (String url, String \[] mediaType, Class  result) ... the script makes a Http Get request and returns a JSON as a String, then it reads the returned JSON and populates the data in the table shown in the figure below

```java
 public Response actionIndex() throws IOException, IllegalArgumentException, IllegalAccessException{

Resttest model = new Resttest();
model.load();
ResttestView view = new ResttestView();

String json = Core.httpGet("https://jsonplaceholder.typicode.com/posts", new String[] {}, String.class);

model.setTable_1(new ArrayList<Resttest.Table_1>());

JSONArray aux1 = new JSONArray(json);

for(int i = 0; i < aux1.length(); i ++) {
  JSONObject obj = aux1.getJSONObject(i);
  Resttest.Table_1 row = new Resttest.Table_1();
  row.setQuote(obj.getString("body"));
  row.setSequence("" + obj.getInt("id"));
  row.setTitulo(obj.getString("title"));
  row.setUserid("" + obj.getInt("userId"));

model.getTable_1().add(row);
}
/*----#end-code----*/

view.setModel(model);

return this.renderView(view);
}
```

![Fig. 1](/files/-LFXP_NXfe6rDd-2g7Mf)

## How to consume a JSON/POST&#x20;

The script below allows you to collect the data (Name, Job) of the form shown in the fig. 2 and then make an HTTP POST, invoking an API ... If the operation is successful, it returns the JSON as a String and prints it in a message INFO.

![Fig. 2](/files/-LFmeVuoRA4a3vrPBj4a)

```java
public Response actionGuardar() throws IOException, IllegalArgumentException, IllegalAccessException{

Restpost model = new Restpost();
model.load();

String dados = "{\"name\": \"" + model.getNome() + "\",\"job\": \"" + model.getJob() + "\"}";

javax.ws.rs.core.Response response = 
Core.httpPost(
"https://reqres.in/api/users", 
dados, 
new String[] {"application/json; charset=UTF-8"}, 
"application/josn", 
javax.ws.rs.core.Response.class
);

if(response.getStatus() == 200 || response.getStatus() == 201) {
    String resultPost = response.readEntity(String.class);
    Core.setMessageSuccess();
    Core.setMessageInfo("JSONResult: " + resultPost);
}else {
    Core.setMessageError();
}

return this.redirect("mywebapp","Restpost","index", this.queryString());
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nosicode.gitbook.io/faq/intermediate/how-to-consume-a-json.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
