0

I am using the following code adapted from this so answer

// remove outer bracket
JSONdata = JSONdata.Trim().Trim('[', ']'); 

// remove white space and line breaks except between double qoutes
JSONdata = Regex.Replace(JSONdata.Trim('"').Replace("\\\"", "\""), "(\"(?:[^\"\\\\]|\\\\.)*\")|\\s+", "$1"); 

// add back bracket
JSONdata = "[" + JSONdata + "]";

Then I get data like this:

[
 [
   {"OptionChoice":14151,"OptionText":"Television"},
   {"OptionChoice":14755,"OptionText":"Test[ something ]"}
 ]
 ,{"OptionChoice":361,"OptionText":"Yes"}
]

OR Minified (as it is in my real code)

[[{"OptionChoice":14151,"OptionText":"Television"},{"OptionChoice":14755,"OptionText":"Test[ something ]"}],{"OptionChoice":361,"OptionText":"Yes"}]

Whitespace and line breaks added back for example only. Real data has no whitespace or line breaks

I would like to be able to get keep the outer brackets but remove the set of inner brackets that is not in double quotes.

EDIT: Expected output

[
  {"OptionChoice":14151,"OptionText":"Television"},
  {"OptionChoice":14755,"OptionText":"Test[ something ]"},    
  {"OptionChoice":361,"OptionText":"Yes"}
]

Minified:

[{"OptionChoice":14151,"OptionText":"Television"},{"OptionChoice":14755,"OptionText":"Test[ something ]"},{"OptionChoice":361,"OptionText":"Yes"}]

I must confess that I don't fully understand the regexp that I have used.

… and now I have two problems.

Community
  • 1
  • 1
gooddadmike
  • 2,329
  • 4
  • 26
  • 48
  • 1
    what is d expected output – vks Sep 24 '14 at 14:25
  • 1
    It might be better to use a library like JSON.NET to parse and reformat the JSON instead of manipulating strings – Andrew Whitaker Sep 24 '14 at 14:32
  • It might be, and I will probably end up switching in the future but for now I just want to fix the invalid data. Obviously the best thing is to get the client to pass valid JSON, which I am also doing. – gooddadmike Sep 24 '14 at 14:41

1 Answers1

1
(?!^)\[(?=(?:[^"]*"[^"]*")*[^"]*$)|\](?!$)(?=(?:[^"]*"[^"]*")*[^"]*$)

Try this.See demo.Replace by ``.Do not forget the flags.

http://regex101.com/r/zR2tR4/25

vks
  • 67,027
  • 10
  • 91
  • 124
  • This works, and you just introduced me to a very nice tool. It doesn't however, have a C# code generator. And I believe the syntax for C# regexp is quite a different animal – gooddadmike Sep 24 '14 at 14:51
  • @lazfish it might be different but regex might not be too diff.the application of regex might vary.Anywayz you can get an idea of regex over there :) – vks Sep 24 '14 at 14:54
  • [it finds the enclosing bracket when I try to convert it](http://regexhero.net/tester/?id=94cec8a1-3ebd-41f0-bb30-94475962ced8) – gooddadmike Sep 24 '14 at 15:08
  • @lazfish it is working.there was a space after `]` in your last line.just use the edited version – vks Sep 24 '14 at 15:33
  • Doesn't work at all when actual whitespace and linebreaks removed. – gooddadmike Sep 24 '14 at 16:11
  • This strips square bracket from beginning and end. I want to strip square brackets NOT at beginning or end except in strings – gooddadmike Sep 24 '14 at 17:08