-5

How can i access this json structure given below?

{
   "question":{
      "119380":{
         "email":{
            "-L8o-zzRRTOJQjI4BQZ0":"abc@gmail.com",
            "-L8odhdW2xAnayboUb8h":"abc@gmail.com",
            "-L8ouI_wt8hb_R0GvXZ5":"abc@gmail.com",
            "-L8p8b03ZpoKUQiYU_69":"abc@gmail.com"
         }
      },
      "123541":{
         "email":{
            "-L8whdSxfPa1DGXwtTuD":"abc@gmail.com"
         }
      },

   }
}

I am trying in the following way

 exports.sendspamalertNotification = functions.database
.ref("spam/question").onWrite(event => {
  const original = event.data.val();
  console.log(event.data.val());

Everything is printing in console but how can i access every question_id (numeric string) and email under that question_id (numeric string)?

Actually i m new to this so please sorry if its silly question !

Please provide some solution !

  • What have you tried? What isn't working – Phil Apr 03 '18 at 05:02
  • Actually i stucked because there is no name to access this numeric string, and this numeric string (basically a question_id) that will gonna change everytime! @Phil – VishalParashar Apr 03 '18 at 05:13
  • and the numeric string will be unique every time for sure. – VishalParashar Apr 03 '18 at 05:22
  • this json structure i have posted here is from console i cant use it as you are showing in snippet. @Mohammad Ali Rony – VishalParashar Apr 03 '18 at 05:49
  • exports.sendNotification = functions.database .ref("leads/{push_Id}").onWrite(event => { const original = event.data.val(); const email = event.data.val().email const location = event.data.val().location this is the another example of a code which working fine. in this way i need to access but there is no heading to access the numeric string? – VishalParashar Apr 03 '18 at 05:50

2 Answers2

1

Use square brackets to access properties whose strings are invalid in dot notation:

const someObj = {
  "question": {
    "119380": {
      "email": {
        "-L8o-zzRRTOJQjI4BQZ0": "abc@gmail.com",
      }
    }
  }
}
console.log(someObj.question[119380].email);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • 2
    please vote to close such duplicate – brk Apr 03 '18 at 05:03
  • TypeError: Cannot read property '119380' of undefined at exports.sendspamalertNotification.functions.database.ref.onWrite.event (/user_code/index.js:854:32) at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:54:20 at process._tickDomainCallback (internal/process/next_tick.js:135:7) Above error is coming when i m trying this - exports.sendspamalertNotification = functions.database .ref("spam/question").onWrite(event => { const original = event.data.val(); console.log(original.question[119380].email); @CertainPerformance – VishalParashar Apr 03 '18 at 05:27
0

You can use this code to access dynamic key JSON.

var obj = JSON.parse('{"question" : {"119380" : {"email" : {"-L8o-zzRRTOJQjI4BQZ0" : "abc@gmail.com", "-L8odhdW2xAnayboUb8h" : "abc@gmail.com","-L8ouI_wt8hb_R0GvXZ5" : "abc@gmail.com","-L8p8b03ZpoKUQiYU_69" : "abc@gmail.com"}},"123541" : {"email" : {"-L8whdSxfPa1DGXwtTuD" : "abc@gmail.com"}}}}');
Object.keys(obj.question).forEach(function(questionKey){
    var question = obj.question[questionKey];
    console.dir(question);
    Object.keys(question.email).forEach(function(emailKey){
        var value = question.email[emailKey];
        console.dir( question.email[emailKey]);
    });
});
Mohammad Ali Rony
  • 4,695
  • 3
  • 19
  • 33