2

I have a JSON string and need to find an nested object and only want the object matching my regexp returned.

{
  "some": {
    "nested": {
       "stuff": [
         {
           "bla": "blub1",
           "bar": "bar"
         },
         {
           "bla": "blub2",
           "bar": "foo"
         },
         {
           "bla": "blub3",
           "bar": "foobar"
         }
      ]
   }
}

I almost got it with this reg exp /{(.*?"bar": "foo".*?)}/gs but this does not return only the matching object.

I only want it to return this:

{
    "bla": "blub2",
    "bar": "foo"
}

See: https://regex101.com/r/mK3oI6/3

I actually don't want to use regex but I'm trying to find the best solution to find a object in a nested object and thought it might work good with regex.

EDIT: I unterstand that this is not a good approach but I just want to try and see the performance between regex and parsing the JSON and and make a deep search

4
  • 1
    regex really isn't the tool to work with json. For instance, a naive implementation will break when JSON string values contain the } character. More robust implementations are possible but can't handle every possible case, and are generally not worth the hassle. Maybe you could tell us what environment you're in so that we suggest another more appropriate tool? Commented Aug 24, 2016 at 11:21
  • 1
    Don't use regex for scenarios like this. Just parse your json string to a json object and traves through the object to get the part you want. You can write a match function to it. Commented Aug 24, 2016 at 11:33
  • What exactly are you trying to match? Commented Aug 24, 2016 at 13:54
  • As described: the nested object which has the property "bar" with the value "foo" Commented Aug 24, 2016 at 14:22

1 Answer 1

4

If you are deadset on using regex:

[^{]*?({\s+"bla[^}]*?})

basically, does a non greedy search until the first { <space> "bar, captures until the first closing curly brace.

edit: if you want only the Nth instance, [^{]*?(?:{\s+"bla[^}]*?}){N}[^{]*?({\s+"bla[^}]*?})

if you want only the one with bar : foo:

.*{([^}]*bar.*?foo"[^}]*?)}

USING JSON WITH REGEX IS A BAD IDEA. becomes fickle and could break easily. It would be a better idea to use one of the primitive iterable types, load the json using the json library, and simply index your way through it to get the data you want.

Sign up to request clarification or add additional context in comments.

4 Comments

First regex matches all of them and not only the one with "foor": "bar". I actually don't want to use regex, I'm trying to find the best solution to find an object in an nested object.
@Heiko Then why did you put "with regexp" in the title of your question?
Because I want to try different approaches and with regexp is one of them.
the second one matches only the one with bar : foo, and i've added a third that will match foo : bar based on the words

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.