How to parse AppsFlyer Conversion Data in Javascript?

193 views Asked by At

How to parse this response:

{af_sub1=1.5, af_deeplink=true, campaign=None, media_source=None, install_time=2018-05-08 03:34:34, af_status=Non-organic, path=, scheme=, host=}

as result of following code:

var onSuccess = function(result) {
     console.log(result);
};

window.plugins.appsFlyer.initSdk(options,onSuccess,onError);

It's look like JSON, but using = instead of :, then I tried to parse it using result.af_sub1 and result["af_sub1"], both return undefined

1

There are 1 answers

0
Jan Wendland On BEST ANSWER

You could first transform the response to conform to JSON and then parse it using JSON.parse. See the snippet below. Note that all data types will be strings and you need to take care of data type conversions yourself. I.e. af_deeplink will not be a boolean.

let result = "{af_sub1=1.5, af_deeplink=true, campaign=None, media_source=None,install_time=2018-05-08 03:34:34, af_status=Non-organic, path=, scheme=, host=}";

let parseResponse = (res) => JSON.parse(res.replace(/([^,\s\{=]+)=([^,]*)(?=,|\})/gi, '"$1" : "$2"'));
let obj = parseResponse(result);

console.log(typeof obj);  // object
console.log(obj.af_sub1); // 1.5

PS: For detailed explanations on the used regular expression please refer to the snippet I created on regex101.com