Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can JSON files be parse into name: value?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading