2

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!

4
  • 3
    Why don't you fix the source so it generates valid JSON. Anything you do to try to clean it up won't be reliable. Commented Jul 20, 2020 at 22:42
  • 1
    Please show the desired result for your example string. If possible, please also reduce the length of the example string to a minimum while still retaining its structure. Commented Jul 20, 2020 at 22:53
  • Well, just looking at that JSON I can already tell you that something like " Shopify ":, expects a value assigned as the colon suggests, which a value doesn't exist. You will get an error there. I'd do what @Barmar suggested which is addressing why it's giving bad JSON in the first place. Commented Jul 21, 2020 at 0:20
  • It's easy to do by looking at it, because humans are good at pattern matching. But to program it you have to come up with concrete rules that match everything you want to fix, and doesn't match something that should be left alone. That's hard. Commented Jul 21, 2020 at 0:21

1 Answer 1

1

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));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.