1

I have a JSON String, like "{"Key1": "Value1", "Key2": "Value2"}"

And I'm trying to specifically get Value1 (I don't want the quotes)

Basically I don't want to decode the entire JSON string, I just want to extract a single value as a string

The best I can do right now is get "Key1": "Value1"


There are a bunch of questions that are similar, and here are answers that are close:

https://stackoverflow.com/a/14350155/2415178
https://stackoverflow.com/a/33783638/2415178

However, each of these answers always include the key name as well

I've been fiddling with regexr (here's my test: https://regexr.com/57fln), but I've been at it for like a half hour now and I don't really want to keep trying as I every time I learn regex I end up forgetting it because of how infrequently I use it. I'm assuming it should be pretty simple, but my solutions that are close look really ugly

A O
  • 5,516
  • 3
  • 33
  • 68
  • 1
    "Basically I don't want to decode the entire JSON string" why? This reads like https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Toastrackenigma Jun 28 '20 at 05:33
  • so specifically, we are doing a lot of client logging around socket messaging. instead of adding log messages everywhere, we want just 1 logging one *right* before a socket message is sent or received (at this point we are dealing with strings). And we want to label these messages based on a route. It's wasted effort to decode the string again. You may wonder then why not pull the value before encoding the object into a string? but we also have a queueing system for messages (Strings), which would need to also retain values for logging purposes. anyways this is mostly for dev builds – A O Jun 28 '20 at 05:42
  • @CarySwoveland to clarify, at this specific point I do not have a hash map, I only have a string. I want the value for logging purposes, so there is no reason to validate if it the key/value exists, and I don't need the key. To be more explicit, we have socket messages with routes like CHAT_CLIENT_SEND_MESSAGE and CHAT_CLIENT_JOIN_ROOM. Just want to see all socket message events with one line of code in one place, so parsing the message string is the easiest – A O Jun 28 '20 at 09:43
  • 1
    I see. Thanks for the explanation. I deleted my comment and will delete this one when I figure you’ve seen it. – Cary Swoveland Jun 28 '20 at 09:49
  • i've seen it! no worries :) i know it's a very strange ask, it's specifically because it's just debug code. these lines will be precompiled out with production code. we just have a bunch of beta users and want to see at a high level all of their socket messages – A O Jun 29 '20 at 06:51

1 Answers1

3

The RegEx that you link to,

/"Key1": "(.+?)"/i

is actually perfect - it's just that you need to get the output of the capture group, rather than of the whole RegEx, and the tool that you're using (RegExr) doesn't show this.

If you ask for just that group, then you'll have what you wanted, e.g. in JavaScript:

window.onload = ()=>{
  document.write('{"Key1": "Value1", "Key2": "Value2"}'.match(/"Key1": "(.+?)"/i)[1]);
}

Note: the [1] is doing the magic here - if you did [0] you would get the whole capture string - which is what RegExr shows. [1] shows the 1st capture group (if one exists), [2] would show the second, etc.


However, I would recommend that you use a JSON parser instead, as there are a lot of things that this could break on - e.g. if the value has escaped quotes like "Key1": "Value\"1", etc.

Toastrackenigma
  • 7,604
  • 4
  • 45
  • 55
  • omg so many times when I was playing with regexr I was just thinking damn I see here they have the capture group I want, but if only I could reference it haha. I'm in Swift, I'm sure I can do this, will report back shortly – A O Jun 28 '20 at 05:44
  • @AO pretty sure Swift lets you do this, otherwise I guess another solution would be something like this: https://stackoverflow.com/q/3926451/2691058 – Toastrackenigma Jun 28 '20 at 05:48
  • building right now, I'm sure it'll work, and yeah I tried to play with the lookaround in regexr but was having a hard time getting results even though the documentation seemed pretty clear – A O Jun 28 '20 at 05:49
  • Yep, all set! I used this handy extension, and we're perfect :) https://stackoverflow.com/a/53652037/2415178 thanks for your time! – A O Jun 28 '20 at 05:59