2

I have JSON text like this :

 ...
 "simples":{
    "AS100ELABQVKANID-91057":{
       "meta":{
          "sku":"AS100ELABQVKANID-91057",
          "price":"3669000.00",
          "original_price":"3379000.00",
          "special_to_date":"2015-03-19 23:59:59",
          "shipment_type":"1",
          "special_price":"3299000.00",
          "tax_percent":"10.00",
          "sourceability":"Sourceable",
          "quantity":"15",
          "variation":"...",
          "package_type_position":"0",
          "min_delivery_time":"1",
          "max_delivery_time":"3",
          "attribute_set_name":"electronics",
          "3hours_shipment_available":false,
          "estimated_delivery":"",
          "estimated_delivery_position":""
       },
       "attributes":{
          "package_type":"Parcel"
       }
    }
 },
 "description":  
 ...

The above text appears repeatedly in my JSON text. I am trying to build every result to this :

"simples":[],"description"

So far, I have made this regex :

\"simples\":{(?:.*v(?:|=)|(?:.*)?)},\"description\"

But the result is cut everything from my first "simples" into last "description". Regex newbie here.

Thanks in advance

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 3
    I don't think using Regex is the best approach. However you could look into the greedy operators. – Myrtle Mar 21 '14 at 07:32
  • 1
    I have edited the JSON layout. Next time do us a favor and go through http://jsonformatter.curiousconcept.com/#jsonformatter when posting. – Jan Doggen Mar 21 '14 at 07:35

2 Answers2

3

I recommend parsing the JSON, replacing the value, then re-stringifying it

var obj = JSON.parse(json);
obj.simples = [];
json = JSON.stringify(obj);

Using a regexp for this is pure insanity

Mulan
  • 129,518
  • 31
  • 228
  • 259
  • 1
    I can't find JSON.parse. Do I need another reference? BTW, I am using C# and Json.Net – user2080904 Mar 21 '14 at 07:39
  • 1
    @user2080904 That is because this code is Javascript. Your question originally didn't specify what language you were using. If you're looking for how to do it with Json.Net, see my answer. – Brian Rogers Mar 21 '14 at 15:12
-1

Don't use Regex to parse JSON; use a JSON parser.

Here is how you can do this using JSON.Net, assuming the object containing simples is part of a flat list of results:

JArray array = JArray.Parse(json);
foreach (JObject obj in array)
{
    obj["simples"].Parent.Remove();
}
json = array.ToString();

If your JSON is more complicated (i.e. "simples" can appear at more than one level in the JSON), then you will need a recursive search to find and remove it. This answer has a helper method that can find a specific property by name anywhere in the JSON and return a list of all occurrences. Once you have the list of occurrences, you can then loop through them and remove them as shown above.

Community
  • 1
  • 1
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300