I have a URL that looks something like this after decoding using decodeURIComponent
https://secretStar.22.test.com/l/{"mode":"test","app":"revenue:app","param2":1,"loaded":{"APPLICATION@markup://revenue:app":"unique_identifier"},"pathPrefix":"","xx":1}/script22.js
Now, I would like to extract few details from this URL , for Ex:
mode = test
app = revenue:app
param2 = 1
appMarkupRevenueApp = unique identifier
scriptName = script.js
I could have extracted it using qs params if these values were qs params. I was able to extract the information by writing a function that splits and joins and then splits again but that is not very efficient when it comes to extracting these params from more than 4k urls in a csv file.
Is there a better way to extract these? I can think of regex but I am not very familiar with that and could not get that to work.
You can do this by matching the embedded JSON and then turning that into an object, perhaps with something like this:
JSON.parse(foo.match(/({.+})[^}]+/)[1])
The regex /({.+})[^}]+/
creates a group by matching a {
followed by anything at all, followed by }
and then matches only if the group is followed by something that isn't }
The JSON is parsing the first matched group.
There is one problem, that I'm not sure is due to your question having a typo or something to do with decodeURIComponent
:
In your expected output you have:
appMarkupRevenueApp = unique identifier
However your data has:
"APPLICATION@markup://revenue:app":"unique_identifier"
This is quite a mismatch and it is not obvious which is correct
Small demo:
var string = 'https://secretStar.22.test.com/l/{"mode":"test","app":"revenue:app","param2":1,"loaded":{"APPLICATION@markup://revenue:app":"unique_identifier"},"pathPrefix":"","xx":1}/script22.js';
var data = JSON.parse(string.match(/({.+})[^}]+/)[1]);
for(key of Object.keys(data)) { console.log(`${key}: ${data[key]}`) }