1

I'm trying to parse a simple JSON object.

{"difficult:name and value with commas":"99,9%;100,00%","b":"very important value","date":"01/01/2023 21:00:00","object members":4}

My pattern"([^\[\]\{\}]+)":"*([^\[\]\{\}]+)"* doesn't return the result I expect.

By excluding the symbols "[]{}", I want to exclude values that contain arrays or objects, and I need just a simple value pair.

I'd like to have four matches and two submatches (name,value) for each match.

match submatch1 submatch2 comments
"difficult:name and value with commas":"99,9%;100,00%" difficult:name and value with commas 99,9%;100,00% name and a string value I'd like to get without covering quotes "", but It's a guess that values may contain inner quotes
"b":"very important value" b very important value also, it can be a comma inside the value, if it's not restricted by JSON standard
"date":"01/01/2023 21:00:00" date 01/01/2023 21:00:00
"object members":4 object members 4

This is the result of my torment at https://regex101.com/:

Enter image description here

I use VBA, but it's more about the pattern.

1
  • @InSync, thank you very much for the link. It is not answering the question, but a very good solution as well. Thanks a lot again. Commented Sep 23, 2023 at 10:14

1 Answer 1

3

This may work:

"([^"]+)":("([^"]+)"|([^,]+))(?=,|}) 

This how you could use it in Python:

import re json_str = '{"difficult:name and value with commas":"99,9%;100,00%","b":"very important value","date":"01/01/2023 21:00:00","object members":4}' pattern = r'"([^"]+)":("([^"]+)"|([^,]+))(?=,|})' matches = re.findall(pattern, json_str) for match in matches: key = match[0] value = match[2] if match[2] else match[3] print(f'Key: {key}, Value: {value}') 

You can check it out at here

But Parsing JSON using a dedicated JSON parser is generally faster and more efficient than using regular expressions.

Here is a example of how you could parse JSON in Python:

import json json_str = '{"difficult:name and value with commas":"99,9%;100,00%","b":"very important value","date":"01/01/2023 21:00:00","object members":4}' data = json.loads(json_str) for key, value in data.items(): print(f'Key: {key}, Value: {value}') 
Sign up to request clarification or add additional context in comments.

6 Comments

this works, thank you. It gives an additional value, but it's ok for me. Thank you!
@Mik If you're using PHP/PCRE you could use a branch reset to spare one group (updated demo).
@Mik if it works for you can make it as answer but i would suggest you use a json parser instead of using regex
Thank you. I'm still thinking about the to parse )
I have added a example of how you could use json parser in python.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.