Home > Software engineering >  How to read data from nested array in Firestore
How to read data from nested array in Firestore

Time:12-19

I have the below structure in my Firestore and I want to read data and store it in ArrayList like I have "amountArrayList" which will read data from the "transactions" field in Firestore, I want to read all the "amount" fields from "transactions" field and make array list of it so that I can show it in list manner. Firestore structure image

My code

Map<String, Object> map = document.getData();
for (Map.Entry<String, Object> entry : map.entrySet()) {
    if (entry.getKey().equals("transactions")) {
        System.out.println(entry.getValue().toString());
    }
}

Output

[{transactionType=Credit, amount=3000, dateToStr=17/12/2021, timeToStr=08:06:10, description=}, {transactionType=Credit, amount=2000, dateToStr=17/12/2021, timeToStr=08:06:50, description=}]

CodePudding user response:

Since transactions is an array field, the value you get from entry.getValue() is a List of objects. Since each of these objects in the JSON has properties, they each will be a Map<String, Object> again in the Java code.

A simple way to print the amounts would be something like:

List transactions = document.get("transactions");
for (Object transaction: transactions) {
  Map values = (Map)transaction;
  System.out.println(values.get("amount")
}

CodePudding user response:

While Frank van Puffelen's answer will work perfectly fine, there is a solution in which you can directly map the "transactions" array into a list of custom objects. Assuming that you have a class declaration that looks like this:

class User {
    public String balance, email, firstname, lastname, password, username;
    public List<Transaction> transactions;

    public User(String balance, String email, String firstname, String lastname, String password, String username, List<Transaction> transactions) {
        this.balance = balance;
        this.email = email;
        this.firstname = firstname;
        this.lastname = lastname;
        this.password = password;
        this.username = username;
        this.transactions = transactions;
    }
}

And one that looks like this:

class Transaction {
    public String amount, dateToStr, description, timeToStr, transactionType;

    public Transaction(String amount, String dateToStr, String description, String timeToStr, String transactionType) {
        this.amount = amount;
        this.dateToStr = dateToStr;
        this.description = description;
        this.timeToStr = timeToStr;
        this.transactionType = transactionType;
    }
}

To get the list, it will be as simple as:

docRef.get().addOnCompleteListener(task -> {
    if (task.isSuccessful()) {
        DocumentSnapshot document = task.getResult();
        if (document.exists()) {
            List<Transaction> transactions = document.toObject(User.class).transactions;
            List<String> amountArrayList = new ArrayList<>();
            for(Transaction transaction : transactions) {
                String amount = transaction.amount;
                amountArrayList.add(amount);
            }
            // Do what you need to do with your amountArrayList
        }
    }
});

You can read more info in the following article:

  • Related