0

Hey I need a regular expression to replace the matching string with empty.

I have below json

{"session":{"convener_id":null,"convergence":false,"created_at":"2012-06-02T10:00:00","event_id":null,"id":42,"name":"Test Session","next_steps":"","notes":"","room":null,"status":0,"summary":"","time":"10:0","updated_at":"2013-06-03T11:49:45.3397898+05:30","people":null,"comments":null,"convener":null}}

I need a json to replace the null properties to empty so that I don't have any null properties. If any proeprties is null just remove that property. I know regular expression can do the tricks but I am not well versed in regular expression.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
vinod8812
  • 645
  • 10
  • 27
  • You are looking for some pain... Properly parsing nested braces (and correctly handing strings with them i.e. `"field:"b{a{a:null"`) is not easy with regular expressions. Please check answer by Sam on balancing braces in [all time favorite on regular expression](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454) – Alexei Levenkov Jun 03 '13 at 07:07
  • Can you do that with Javascript? Then all you need to do is `delete obj.property` where it is null. Otherwise you will probably need to parse json, there is a project called JSON.NET that can help you. – BrunoLM Jun 03 '13 at 07:18

1 Answers1

3

I would suggest you to don't look at Regex for this case, instead use JSONParser for .Net. This thread can help you on this issue.

Setting JSONSerializer.NullValueHandling to NullValueHandling.Ignore should resolve your issue.

But if still want to treat JSON response as a plain text and want to do string operations, a call to REPLACE (with empty string) using this regex can help you:

(\"[^"]+\":null,?|,?\"[^"]+\":null)
Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56