Reputation: 505
I currently have the following JSON string:
'{"ECommerce ":{" Shopify ":," Magento ":," WooCommerce ":," Squarespace ":},"Tools ":{" Grunt ":," Gulp ":," Vagrant ":},"Containers ":{" LXC ":," Docker ":," Rocket ":},"Digital ":{" SEO ":," Email Marketing ":," Inbound Marketing ":," Crowdfunding ":," Content Distribution ":," Display Advertising ":," Ad Planning and Buying ":," Article Writing ":," SEM ":," Customer Relationship Management ":," Viral Marketing ":," Market Research ":," Social Media ":," Affiliate Marketing ":," Lead Generation ":},"Performance ":{" LoadStorm ":," httperf ":," JMeter ":," LoadUI ":," Blazemeter ":," LoadImpact ":," Nouvola ":," LoadRunner ":," Soasta CloudTest ":},
which somehow has semi-colons, quotes and an extra curly bracket mixed inside {}. I want to get rid of these so I can convert this to a Python dict, and my question is, is there a way to use regex to get rid of the extraneous characters (so the ": and { characters) found within these brackets {} (so as to leave the first semi-colon after what would be the first key "ECommerce").
I've bolded the characters that I believe would throw a JSONDecodeError:
'{"ECommerce ":{" Shopify ":," Magento ":," WooCommerce ":," Squarespace ":}
If that isn't possible, what other methods could I use to deal with this?
Thank you!
Upvotes: 2
Views: 1393
Reputation: 4912
const string = '{"ECommerce ":{" Shopify ":," Magento ":," WooCommerce ":," Squarespace ":},"Tools ":{" Grunt ":," Gulp ":," Vagrant ":},"Containers ":{" LXC ":," Docker ":," Rocket ":},"Digital ":{" SEO ":," Email Marketing ":," Inbound Marketing ":," Crowdfunding ":," Content Distribution ":," Display Advertising ":," Ad Planning and Buying ":," Article Writing ":," SEM ":," Customer Relationship Management ":," Viral Marketing ":," Market Research ":," Social Media ":," Affiliate Marketing ":," Lead Generation ":},"Performance ":{" LoadStorm ":," httperf ":," JMeter ":," LoadUI ":," Blazemeter ":," LoadImpact ":," Nouvola ":," LoadRunner ":," Soasta CloudTest ":},';
const json = string
.replace(/ /g, '') // remove excess spaces
.replace(/(?!^){/g, '[') // replace braces (except the first) with brackets
.replace(/}/g, ']') // replace closing braces with brackets
.replace(/:]/g, ']') // remove erroneous colons before brackets
.replace(/:,/g, ',') // remove erroneous colons before commas
.replace(/.$/, '}'); // replace last comma with bracket
console.log(JSON.parse(json));
Upvotes: 1