I'm having difficulty parsing a JSON string with nested arrays. here is an example of the JSON
const allHeaders = {"Strict-Transport-Security":"max-age=31536000","Timing-Allow-Origin":"*","s_ip":"1.5.295.221","Server":"Asia","Vary":"Accept-Encoding","x-application-context":"shop-buyer-s.7001","s_group":"buyer-sessio","Content-Language":"zh-CN","Access-Control-Allow-Headers":"Origin, x-ua, x-umidtoken, x-csrf-token, X-Requested-With, Content-Type, Accept","Set-Cookie":["hms_cid=80dcf576-a581-4d43-8302-1605fe36565c; Domain=.hms.zn; Expires=Sun, 09-Jul-2023 04:39:13 GMT; Path=/","hms_sid=112120422020a413958ddf33139b516b; Domain=.hms.zn; Path=/; HttpOnly","_tb_token_=e8eafb9bebe63; Domain=.hms.zn; Path=/; HttpOnly"],"Transfer-Encoding":"chunked","object-status":"ttl=2592000,age=42,gip=121.43.99.68","Connection":["keep-alive","Transfer-Encoding"],"s_tag":"285873024335988|0^|^^","s_tid":"21410a7016573415536295456ef332","eagleeye-traceid":"21410a7016573415536295456ef332","s_v":"4.0.2.0","Content-Encoding":"gzip","P3P":"CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR'","Date":"Sat, 09 Jul 2022 04:39:13 GMT","Access-Control-Max-Age":"3600","s_ucode":"Asia:CENTER","Access-Control-Allow-Methods":"POST, GET, OPTIONS, DELETE","Content-Type":"text/html;charset=UTF-8","s_status":"STATUS_NOT_EXISTED"}
I need to get the number hms_sid= exmple: 112120422020a413958ddf33139b516b
I'm trying
const sid = allHeaders['Set-Cookie']
CodePudding user response:
I'm pretty sure google apps script is just javascript so here's my code:
let cookie = allHeaders['Set-Cookie'][1];
let tokens = cookie.split(";");
let sidString = tokens[0].split("=");
let sid = sidString[1];
Here is how I did it:
Given:
const allHeaders = {
"Strict-Transport-Security": "max-age=31536000",
"Timing-Allow-Origin": "*",
"s_ip": "1.5.295.221",
"Server": "Asia",
"Vary": "Accept-Encoding",
"x-application-context": "shop-buyer-s.7001",
"s_group": "buyer-sessio",
"Content-Language": "zh-CN",
"Access-Control-Allow-Headers": "Origin, x-ua, x-umidtoken, x-csrf-token, X-Requested-With, Content-Type, Accept",
"Set-Cookie": ["hms_cid=80dcf576-a581-4d43-8302-1605fe36565c; Domain=.hms.zn; Expires=Sun, 09-Jul-2023 04:39:13 GMT; Path=/", "hms_sid=112120422020a413958ddf33139b516b; Domain=.hms.zn; Path=/; HttpOnly", "_tb_token_=e8eafb9bebe63; Domain=.hms.zn; Path=/; HttpOnly"],
"Transfer-Encoding": "chunked",
"object-status": "ttl=2592000,age=42,gip=121.43.99.68",
"Connection": ["keep-alive", "Transfer-Encoding"],
"s_tag": "285873024335988|0^|^^",
"s_tid": "21410a7016573415536295456ef332",
"eagleeye-traceid": "21410a7016573415536295456ef332",
"s_v": "4.0.2.0",
"Content-Encoding": "gzip",
"P3P": "CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR'",
"Date": "Sat, 09 Jul 2022 04:39:13 GMT",
"Access-Control-Max-Age": "3600",
"s_ucode": "Asia:CENTER",
"Access-Control-Allow-Methods": "POST, GET, OPTIONS, DELETE",
"Content-Type": "text/html;charset=UTF-8",
"s_status": "STATUS_NOT_EXISTED"
}
I pulled the cookie the same that you did:
let cookie = allHeaders['Set-Cookie'];
The issue with this is that it returns:
[
'hms_cid=80dcf576-a581-4d43-8302-1605fe36565c; Domain=.hms.zn; Expires=Sun, 09-Jul-2023 04:39:13 GMT; Path=/',
'hms_sid=112120422020a413958ddf33139b516b; Domain=.hms.zn; Path=/; HttpOnly',
'_tb_token_=e8eafb9bebe63; Domain=.hms.zn; Path=/; HttpOnly'
]
Using typeof
I was able to determine this is an Object
type.
let cookie = allHeaders['Set-Cookie'];
console.log(typeof(cookie));
object
Since this is an Object I enumerated the keys:
console.log(Object.keys(cookie));
[ '0', '1', '2' ]
Now I'm able to match the comma separated values with an object key. This means I can get only the cookie value with the info I need:
let cookie = allHeaders['Set-Cookie'][1];
Which outputs:
hms_sid=112120422020a413958ddf33139b516b; Domain=.hms.zn; Path=/; HttpOnly
After that I checked the type of the object again with typeof
to find out it's a String
so I used .split(";")
string function to tokenize the string:
[
'hms_sid=112120422020a413958ddf33139b516b',
' Domain=.hms.zn',
' Path=/',
' HttpOnly'
]
we only care about the first token and we want to tokenize that again by =
:
let sidString = tokens[0].split("=");
[ 'hms_sid', '112120422020a413958ddf33139b516b' ]
And there we have it, we can get the value with sidString[1]
CodePudding user response:
It is unlikely that hms_sid
would always appear as the second tuple in the cookie list. Use Array.filter()
to locate it, then get the first matching cookie and extract the value with String.match()
, testing for missing values, like this:
function test() {
console.log(getCookieTokenValue_(allHeaders, 'hms_sid'));
}
function getCookieTokenValue_(headers, token) {
const tokenRegex = new RegExp('\\b' token '=(\\w )');
const cookies = headers['Set-Cookie'];
const tokens = cookies.filter(cookie => cookie.match(tokenRegex));
return tokens.length ? tokens[0].match(tokenRegex)[1] : null;
}