2

I need to trim image filenames inside json to empty values, so I'v started to try with regex. There are different image tags in json but they all end with "_url" suffix. Tried various combinations with regex but with no luck. How such regex could look like?

Here's what I have:

{ "list" : 
  [
    {
        "id" : 123,
        "name" : "some name",
        "description" : "Lorem ipsum",
        "logo_image_url": "222.png",
        "content_image_url" : "1355.png"},

    {   
        "id" : 123,
        "name" : "some other name",
        "description" : "Lorem ipsum",
        "logo_image_url": "111.png",
        "content_image_url" : "1355.png"
    }
  ]
}

And this is what I need to make:

{ "list" : 
  [
    {
        "id" : 123,
        "name" : "some name",
        "description" : "Lorem ipsum",
        "logo_image_url": "",
        "content_image_url" : ""},

    {   
        "id" : 123,
        "name" : "some other name",
        "description" : "Lorem ipsum",
        "logo_image_url": "",
        "content_image_url" : ""
    }
  ]
}
Centurion
  • 14,106
  • 31
  • 105
  • 197
  • `Tried various combinations with regex but with no luck` Please post what you have tried, otherwise you just seem like a helpvampire. On another note, editing JSON with regex is generally a **big NO**. I suggest to use a proper JSON parser. – HamZa Jul 18 '13 at 13:58

1 Answers1

1

This id doable with an expression like this:

("(?:logo_image_url|content_image_url)"\s*:\s*)".*?", replace with \1""

Though as pointed out by @HamZa: editing JSON with regex is generally a big no-no.

Demo+explanation: http://regex101.com/r/oY3mJ9

Firas Dib
  • 2,743
  • 19
  • 38