Got more questions? Find advice on: ASP | SQL | XML | Windows
Welcome to RegexAdvice Sign in | Join | Help

Minify a JSON String (clearing all Spaces, New-lines outside quotes)

  •  05-07-2010, 8:42 AM

    Minify a JSON String (clearing all Spaces, New-lines outside quotes)

    Hi guys :)

    So, i'm not the RegEx expert, but i think for one who make this more often than me it, the problem could be easy.

     

     

    1. I'm using JavaScript with the normal Regex Object

    2. I often got different JSON strings which i wanna store in a Database. But for saving some data i wanna "compress" them. so i need a replacement method to clear out all Spaces and New-lines outside quoted strings. Regex seems to me for the best method.

    3. So at least here an example JSON string which i have and which i wanna have:

    I HAVE

    {
        "  glossary   "    :    {
            "title": "ex :    [ ample:  \"  : \"  {  \"   [  \"   ,  glossary"     ,
            "GlossDef":{     
                "para": "A\" :   \"   , meta-markup language, used to create markup languages such as DocBook.",
                "GlossSeeAlso" :[
                    " G M L ",   
                    "XML"
                ]
            }
        }
    }

    I WANNA HAVE

    {"  glossary   ":{"title":"ex :    [ ample:  \"  : \"  {  \"   [  \"   ,  glossary","GlossDef":{"para":"A\" :   \"   , meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":[" G M L ","XML"]}}}

     

    I tried a lot but i always stucked  with spaces inside quotes (that they will not be matched!). Don't wonder about the JSON string it's just an example with mostly all situations in it ;)

     

    greetz

    Chris

    Filed under: , ,
View Complete Thread