0
function _quotedText(data, config) {
    var pathKeys=config.customKey;  
    console.log(pathKeys);         //prints os_platform
    var inAuth_data=(JSON.parse(JSON.parse(JSON.parse(data["event.fields.custom_fields.inauth_device_data"])), (key, value) => {
        if (key == pathKeys)
        {
            console.log(value);     //prints Android
            return value;           //returns undefined
        }
    }));

    console.log(inAuth_data);       //prints undefined
    return inAuth_data;             //returns undefined
}

I have read other similar questions and tried what they are doing but i still don't get it. Why is it returning undefined ?

The JSON in question here is:

"\"{\\\"deviceInfo\\\":{\\\"permanentId\\\":\\\"23434433-3333-4444-9581-f9cb641d28f5\\\",\\\"publicSigningKey\\\":\\\"MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEAzMRW6jXVgqX0QV0EA9h2XnnPvntER5yqPKvD+yLKtxXzBCYMzygEM1nlwBRZhVpNJFvzZ2X+oTLGasdbasjhddasdu0PAWIc1AqKMt6rDEJv4a8bgqqAnXXnvR/QjwtsIq3T59LqivcoB2IPGq7Mof7yRJXKtrEOK2a1b8ixWJ5MBZ06drONhMkzeDTKjenMSM0Hf3BFTlXKCFaZbfShr1OK+wMqWFYrAJsAsufvzwjxKzaZ/RspVuZtfHo0g0z7SxWRBT7+2lKGN0pFyUYebN471n5hZxVMY8Zjfr75QUK1fWIVhzNRc+pH3PhakBeqsnmNncy+XOA7TwIBEQ==\\\"},\\\"objects\\\":[{\\\"perm_id\\\":\\\"READ_PHONE_STATE_DISABLED\\\",\\\"device_pid\\\":\\\"READ_PHONE_STATE_DISABLED\\\",\\\"sdk_version\\\":\\\"Android-MME-7.8.2\\\",\\\"data\\\":{\\\"contact_info_logs\\\":[],\\\"wifi_connection_logs\\\":[{\\\"ip\\\":\\\"111.22.33.44\\\",\\\"ssid\\\":\\\"\\\\\\\"iPhone sdf Gucci\\\\\\\"\\\",\\\"linkspeed\\\":\\\"72\\\",\\\"bssid\\\":\\\"22: 56: 23: 45: 0c: ab\\\",\\\"rssi\\\":\\\"-35\\\",\\\"macaddr\\\":\\\"a4: ss: ea: 17: dg: ss\\\",\\\"networkid\\\":\\\"17\\\"}]}}]}\""
Anusha
  • 204
  • 1
  • 4
  • 12
  • 1
    Why are you parsing the same data so many times? `(JSON.parse(JSON.parse(JSON.parse(` – evolutionxbox Apr 20 '17 at 14:32
  • because the data is stringified thrice ex: ""\"{\\\"deviceInfo\\\":{\\\"permanentId\\\":\\\"8f408412-6934-4bf8-9581-............ – Anusha Apr 20 '17 at 14:34
  • 1
    Ah, it's Mr Thrice! You shouldn't need to parse the data multiple times. It's also causing mismatched braces. Try removing all but one `JSON.parse` – evolutionxbox Apr 20 '17 at 14:40
  • tried that, "console.log(value); //prints Android" now prints nothing. – Anusha Apr 20 '17 at 14:43
  • Can you update the question with a [mcve] of the JSON? – evolutionxbox Apr 20 '17 at 14:44
  • But when I parse it thrice and do a console.log(value) it print the correct value, however when I try to return it, it return undefined! – Anusha Apr 20 '17 at 14:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142196/discussion-between-evolutionxbox-and-anusha). – evolutionxbox Apr 20 '17 at 15:16
  • Take a look at [this SO Answer](http://stackoverflow.com/questions/30386685/why-would-a-return-be-undefined-but-console-log-return-an-int) – Rüzgar Apr 20 '17 at 15:17
  • @evolutionxbox I found the solution dude, appreciate your assistance. – Anusha Apr 20 '17 at 17:27

1 Answers1

0

Found the answer after a lot of head banging. However, it seems like the function was being called for every object in the JSON. And after

if (key == pathKeys)

was checked it printed the value and returned the value, however there were still objects in the JSON string after the matched key which were being overridden onto the variable inAuth_data.

Therefore, all I had to was declare another variable outside the reviver function and assign to it the value when the condition was true.

function _quotedText(data, config) {
var pathKeys=config.customKey;  
console.log(pathKeys);         //prints os_platform
var data = undefined;
var inAuth_data =(JSON.parse(JSON.parse(JSON.parse(data["event.fields.custom_fields.inauth_device_data"])), (key, value) => {
    if (key == pathKeys)
    {
        console.log(value);     //prints Android
        data = value;           //Assigning value of value to data
    }
  }));

console.log(inAuth_data);       //prints Android
return inAuth_data;             //returns Android

}

Thanks to @evolutionxbox for working with me on this.

Anusha
  • 204
  • 1
  • 4
  • 12