# [FIXED] Regex to match key value pairs from JSON ~ JavaFixing

I need to match all the key values pairs from a complex JSON, but only values that are text / String.

For example from:

{"results":[
{"id":16,"name":"some name1","location":"some  location1","parent":true, ,"region":"some region"},
{"id":157,"name":"some name2" , "location":some location2","parent":true}
],"totalCount":170}


I need to match:

"name"
"some name1"
"location"
"some location1"
"region"
"some region1"
etc


I have this [^:]+\"(?=[,}\s]|\$) , but it only matches the values (which are correct).

I need also to match the keys: “name” , “location”, “region” (and there can be other key names)

As others pointed out, if you want a robust solution use a JSON parser in your language.

If you want to use regex, and the engine supports lookbehind you can use this:

/("[^"]*"(?=:")|(?<=":)"[^"]*")/g


Explanation:

• | – or combination of:
• "[^"]*"(?=:") – quote, 0+ non-quotes, quote, followed by positive lookahead for colon and quote
• (?<=":)"[^"]*" – positive lookbehind for quote and colon, followed by quote, 0+ non-quotes, quote

If you want to exclude the quotes in the matches, use this regex:

/(?<=")([^"]*(?=":")|(?<=":")[^"]*)/g


Note that these regexes fail for cover corner cases, such as whitespace around keys and values, escaped quotes in values, etc. Hence it is safer to use an actual JSON parser.