Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
Convert CSS Declaration into a Javascript Object

I've written a script that extracts the @font-face declaration from CSS, and I'd now like to convert it into a javascript object. Here's an example of what the original string will look like:

{font-family:GT Alpina;font-weight:100;src:url(/next/contenthash/next/fonts/gt-alpina/thin/GT-Alpina-Standard-Thin.contenthash.3322f75174e682b58d25cca4c516f38b51edb6ef.woff2) format("woff2");font-display:swap}

Thats the string I'd like to convert into an object ^.

I tried using JSON.parse(), but it doesn't work since it's not a JSON encoded string. Is therea simple way that I'm missing to convert this string into a javascript object, or will I have to loop through it and manually convert it?

Any help is appreciated — thanks!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The key point is using regex to convert CSS to JSON format and then using JSON.parse. A description of each replace part is included in the code.

var css = '{font-family:GT Alpina;font-weight:100;src:url(/next/contenthash/next/fonts/gt-alpina/thin/GT-Alpina-Standard-Thin.contenthash.3322f75174e682b58d25cca4c516f38b51edb6ef.woff2) format("woff2");font-display:swap}';

cssObject = JSON.parse(css
  .replace(/"/g, '\\"')               // add \ before "
  .replace(/\s*\{\s*/, '{"')          // add start "
  .replace(/\s*;?\s*\}\s*/, '"}')     // add end "
  .replace(/\s*:\s*/g, '":"')         // replace : with ":"
  .replace(/\s*;\s*/g, '","')         // replace ; with ","
);

console.log("CSS Object: ", cssObject);

To convert keys to camel format another replace is required as the following code:

var css = '{font-family:GT Alpina;font-weight:100;src:url(/next/contenthash/next/fonts/gt-alpina/thin/GT-Alpina-Standard-Thin.contenthash.3322f75174e682b58d25cca4c516f38b51edb6ef.woff2) format("woff2");font-display:swap}';

cssObject = JSON.parse(css
.replace(/"/g, '\\"')               // add \ before "
  .replace(/\s*\{\s*/, '{"')          // add start "
  .replace(/\s*;?\s*\}\s*/, '"}')     // add end "
  .replace(/\s*:\s*/g, '":"')         // replace : with ":"
  .replace(/\s*;\s*/g, '","')         // replace ; with ","
  .replace(/"([^"]+)":/g, (full, property) => '"' + property.toLowerCase().replace(/-(\w)/, (a, b) => b.toUpperCase()) + '":')
);

console.log("CSS Object with Camel Format: ", cssObject);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!