skip to Main Content

when i fetch the data from firebase real time database, i am getting string object like below. May i know how can i convert this to JSON object?. how can i get the data like Address:"Add line 1, Add line 2, Street,City,State.". ?. This is for my react native app.

What i tried:

  • when i tried to use JSON.parse, i am getting the error ‘JSON Parse
    error: Unexpected token: o’.
  • when i display the data, it is [object object]
  • when i try typeof, i am getting result as object

data that i get from Firebase real time database(It is getting displayed like below when i use JSON.stringify):

user:{"Address":"Add line 1, Add line 2, Street,City,State.","Age":"42","CAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"C1","Slot":"","Treatment":"","extra1":"","extra2":""}},"PAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"P1","Slot":"","Treatment":"","extra1":"","extra2":""},"1":{"AppointDate":"","ConsultDoc":"","Patient":"P2","Slot":"","Treatment":"","extra1":"","extra2":""},"2":{"AppointDate":"","ConsultDoc":"","Patient":"P3","Slot":"","Treatment":"","extra1":"","extra2":""},"3":{"AppointDate":"","ConsultDoc":"","Patient":"P4","Slot":"","Treatment":"","extra1":"","extra2":""}},"name":"Zulu","pass":"1234","phone":"+918888888888"}

how i fetch data from firebase:

const readUserData = async (phoneNumber) => {
try {
      const reference = ref(db, 'x123456/' + phoneNumber);
      return (await get(reference)).toJSON((snapshot) => {
          const data = snapshot.val();
          console.log('DATA: ' + data);
      }, (error) => {
          console.error(error);
          return 0;
      });
}

How i call the above function:

      exist=await readUserData('+91'+phoneNumber);
      console.log('user:'+ JSON.stringify(exist));
    } catch (error) {
      alert(error);
    }```

2

Answers


  1. Maybe this is the answer you want :

    var obj_str = '{"Address":"Add line 1, Add line 2, Street,City,State.","Age":"42","CAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"C1","Slot":"","Treatment":"","extra1":"","extra2":""}},"PAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"P1","Slot":"","Treatment":"","extra1":"","extra2":""},"1":{"AppointDate":"","ConsultDoc":"","Patient":"P2","Slot":"","Treatment":"","extra1":"","extra2":""},"2":{"AppointDate":"","ConsultDoc":"","Patient":"P3","Slot":"","Treatment":"","extra1":"","extra2":""},"3":{"AppointDate":"","ConsultDoc":"","Patient":"P4","Slot":"","Treatment":"","extra1":"","extra2":""}},"name":"Zulu","pass":"1234","phone":"+918888888888"}';
    
    console.log(obj_str);
    
    var obj = JSON.parse(obj_str);
    console.log(obj);

    or..

    var obj_str = '{"user":{"Address":"Add line 1, Add line 2, Street,City,State.","Age":"42","CAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"C1","Slot":"","Treatment":"","extra1":"","extra2":""}},"PAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"P1","Slot":"","Treatment":"","extra1":"","extra2":""},"1":{"AppointDate":"","ConsultDoc":"","Patient":"P2","Slot":"","Treatment":"","extra1":"","extra2":""},"2":{"AppointDate":"","ConsultDoc":"","Patient":"P3","Slot":"","Treatment":"","extra1":"","extra2":""},"3":{"AppointDate":"","ConsultDoc":"","Patient":"P4","Slot":"","Treatment":"","extra1":"","extra2":""}},"name":"Zulu","pass":"1234","phone":"+918888888888"}}';
    
    console.log(obj_str);
    
    var obj = JSON.parse(obj_str);
    console.log(obj);

    JSON required start with ‘{‘ and end with ‘}’.
    (also required ‘key’ enclosed in quotation marks, like "user")

    Login or Signup to reply.
  2. Your function return is an object already.

    So simply you can use your desired property:

    try{
        const myuser = await readUserData('+91' + phoneNumber);
        console.log(myuser.Address);
    } catch (error) {
        alert(error);
    }
    

    More Info

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search