Paulo Roberto
Paulo Roberto

Reputation: 1548

Write Json file the data extracted from a page

I am extracting the data from the table on the page https://br.investing.com/indices/major-indices

I can extract the data perfectly, but I don't know how to write to a Json, in the format below.

Selenium code for data extraction:

public void AcessarPagina() {
    System.out.println("*** Abrindo a Pagina de Extração *** \n");
    driver.get("https://br.investing.com/indices/major-indices");
    WebDriverWait AguardarTabela = new WebDriverWait(driver, 20);
    AguardarTabela.until(ExpectedConditions.presenceOfElementLocated(By
                                           .id("cross_rates_container")));
    System.out.println("*** Iniciando a Extração *** \n");
    WebElement TabelaCabecalho = driver.findElement(By.id("cross_rates_container"));
    List<WebElement> Cabecalho = TabelaCabecalho.findElements(By.tagName("th"));
    for (int i = 0; i < Cabecalho.size(); i++) {
        System.out.print(Cabecalho.get(i).getText() + ";");
    }
    WebElement TabelaDados = driver.findElement(By.id("cross_rates_container"));
    List<WebElement> Linhas = TabelaDados.findElements(By.tagName("tr"));
    List<WebElement> ListaColunas = null;
    for (WebElement row : Linhas) {
        System.out.println();
        ListaColunas = row.findElements(By.tagName("td"));
        for (WebElement column : ListaColunas) {
            System.out.print(column.getText() + ";");
        }
    }
}

Json format I need:

[ {
  "indice" : "Dow 30",
  "ultimo" : "23.185,62",
  "maxima" : "23.189,76",
  "minima" : "21.285,37",
  "variacao" : "+1.985,00",
  "variacao2" : "+9,36%",
  "hora" : "13/03"
}, {
  "indice" : "S&P 500 VIX",
  "ultimo" : "23.185,62",
  "maxima" : "23.189,76",
  "minima" : "21.285,37",
  "variacao" : "+1.985,00",
  "variacao2" : "+9,36%",
  "hora" : "13/03"
} ]

Upvotes: 1

Views: 113

Answers (2)

MinhazMurks
MinhazMurks

Reputation: 301

Mapping the object to a Java object, then using Jackson as Chris mentioned is probably the best bet. I have put together an example of what you can do:

Here is the Java object that could hold your values:

public class TableElement {
    private String indice;
    private String ultimo;
    private String maxima;
    private String minima;
    private String variacao;
    private String variacao2;
    private String hora;

    public String getIndice() {
        return indice;
    }

    public void setIndice(String indice) {
        this.indice = indice;
    }

    // Add all getters and setters ...
}

Note - getters and setters are required because that's what jackson uses to retrieve/set values on an object from and to json

And actually mapping it to json would go something like this:

StockTable stockTable = new StockTable();
List<TableElement> stocks = new ArrayList<>();

TableElement tableElement = new TableElement();
tableElement.setIndice("Dow 30");
tableElement.setUltimo("23.185,62");
tableElement.setMaxima("23.189,76");
tableElement.setMinima("21.285,37");
tableElement.setVariacao("+1.985,00");
tableElement.setVariacao2("+9,36%");
tableElement.setHora("13/03");

TableElement tableElement2 = new TableElement();
tableElement2.setIndice("S&P 500 VIX");
tableElement2.setUltimo("23.185,62");
tableElement2.setMaxima("23.189,76");
tableElement2.setMinima("21.285,37");
tableElement2.setVariacao("+1.985,00");
tableElement2.setVariacao2("+9,36%");
tableElement2.setHora("13/03");

stocks.add(tableElement);
stocks.add(tableElement2);

ObjectMapper objectMapper = new ObjectMapper();

try {
    System.out.println(objectMapper.writerWithDefaultPrettyPrinter()
                                       .writeValueAsString(stocks));
} catch (JsonProcessingException e) {
    e.printStackTrace();
}

And it would produce output that looks like this:

[ {
  "indice" : "Dow 30",
  "ultimo" : "23.185,62",
  "maxima" : "23.189,76",
  "minima" : "21.285,37",
  "variacao" : "+1.985,00",
  "variacao2" : "+9,36%",
  "hora" : "13/03"
}, {
  "indice" : "S&P 500 VIX",
  "ultimo" : "23.185,62",
  "maxima" : "23.189,76",
  "minima" : "21.285,37",
  "variacao" : "+1.985,00",
  "variacao2" : "+9,36%",
  "hora" : "13/03"
} ]

Upvotes: 3

Chris
Chris

Reputation: 1804

Easiest is probably to define a class with those properties (indice etc) and as you parse the rows of data create instances that you add to a List, then just use Gson or Jackson to convert the List of objects to JSON eg How to Create JSONArray for a List<Class name>

Upvotes: 2

Related Questions