I generated a webpage cookie using puppeteer like the following:
[
{
"name": "__Secure-3PSIDCC",
"value": "sec",
"domain": ".youtube.com",
"path": "/",
"expires": 1677041890.31475,
"size": 88,
"httpOnly": true,
"secure": true,
"session": false,
"sameSite": "None",
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
},
{
"name": "SIDCC",
"value": "sec",
"domain": ".youtube.com",
"path": "/",
"expires": 1677041890.314666,
"size": 79,
"httpOnly": false,
"secure": false,
"session": false,
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
}... //and so on
How can I parse the json so it would be name.value
?
What I am doing now is:
const cookies = await page.cookies();
await fs.writeFile('./cookies.json', JSON.stringify(cookies, null, 2));
The thing is youtube does not allow cookies in file...
any solution so I can format the json to name.value
?
The wanted output would be The reseult shold be like this __Secure-3PSIDCC:sec; SIDCC=sec; another=value;
Thanks
CodePudding user response:
Assuming the array in your question is the value in cookies
, you can map the array to the values you want and then join it into the final string
const cookies = [{"name":"__Secure-3PSIDCC","value":"sec","domain":".youtube.com","path":"/","expires":1677041890.31475,"size":88,"httpOnly":true,"secure":true,"session":false,"sameSite":"None","sameParty":false,"sourceScheme":"Secure","sourcePort":443},{"name":"SIDCC","value":"sec","domain":".youtube.com","path":"/","expires":1677041890.314666,"size":79,"httpOnly":false,"secure":false,"session":false,"sameParty":false,"sourceScheme":"Secure","sourcePort":443}]
const cookieString = cookies.map(({ name, value }) =>
`${name}=${value}`).join("; ")
console.log("cookieString:", cookieString)