Need to extract a specific detail from a json - regex

I have the following json which i'm looking to extract the specific string which come after location_code and between quotes XXX123
{"location_code":"XXX123","location_uuid":"XXX-XXX-XXAA-4444-ASDFSDAF44","hotstamp":"1111","card_format":"ABC","accesses":[{"partition_name":"SSSSuljiro","SSSSS":"3","access_levels":["ASDASDASDA"],"location_code":"XXX123"}]}
Would greatly appreciate any help with this!!
I'm using redshift and tried several attempts with regexp_substr

This should work:
(?<="location_code":")\w+
but since you want to parse a json object, there could be better/easier ways to do so.

I believe you could use json_extract_path_text:
select json_extract_path_text(
json_column, -- your json
'location_code', -- json key to extract data from
true -- return null if input is invalid json
);
Make sure that your string is actually valid JSON format.

Related

JMeter Regular extraction looking to correlate a value between two dynamic variables

im in a bit of a pickle
im trying to extract a value where the value before it and after it are very redundant.
[sample of the response with the desired value highlighted][1]
now since the value before it and after it are dynamic that narrows down the regex values i can rely on.
and validating every idea i have gives back a wider string.
how can i extract this value?
[1]: https://i.stack.imgur.com/kYUXf.png
JSON is not a regular language therefore using Regular Expressions for extracting data from it is not the best idea.
Consider using i.e. JSON Extractor which allows using arbitrary JsonPath queries allowing fetching data from JSON in flexible, readable and more powerful way.
If you post the code in text form and not as an image I could come up with a proper JsonPath expression.

Problems with extracting json respnse value in scala using brackets

Have tried using " and ' in different combinations to extract the value 13029416243 from the JSON response body I get in a gatling/scala script
,\"initialString\":\"13029416243\"},
This has been some of my try outs:
.check(regex("initialString(.*?)}").exists.saveAs("initialString"))
and
.check(regex("initialString\\\":\\\"(.*?)\\\"}").exists.saveAs("initialString"))
where the last one results in this output in the log:
---- Errors --------------------------------------------------------------------
regex(initialString\":\"(.*?)\"}).find.exists, found nothing
Any help on how to obtain the value?
You can make the use of lookbehinds. But to be honest. Json should be parsed with json parsers. Regex is not always a reliable tool for searching in jsons.
(?<=initialString\\":\\")\d+\b
https://regex101.com/r/Wgeurd/1/

Regular expression to extract either integer or string from JSON

I am working in an environment without a JSON parser, so I am using regular expressions to parse some JSON. The value I'm looking to isolate may be either a string or an integer.
For instance
Entry1
{"Product_ID":455233, "Product_Name":"Entry One"}
Entry2
{"Product_ID":"455233-5", "Product_Name":"Entry One"}
I have been attempting to create a single regex pattern to extract the Product_ID whether it is a string or an integer.
I can successfully extract both results with separate patterns using look around with either (?<=Product_ID":")(.*?)(?=") or (?<=Product_ID":)(.*?)(?=,)
however since I don't know which one I will need ahead of time I would like a one size fits all.
I have tried to use [^"] in the pattern however I just cant seem to piece it together
I expect to receive 455233-5 and 455233 but currently I receive "455233-5"
(?<="Product_ID"\s*:\s*"?)[^"]+(?="?\s*,)
, try it here.

Parsing JSON style text using sscanf()

["STRING", FLOAT, FLOAT, FLOAT],
I need to parse three values from this string - a STRING and three FLOATS.
sscanf() returns zero, probably I got the format specifiers wrong.
sscanf(current_line.c_str(), "[\"%s[^\"]\",%f,%f,%f],",
&temp_node.id,
&temp_node.pos.x,
&temp_node.pos.y,
&temp_node.pos.z))
Do you know what's wrong?
Please read the manual page on sscanf(3). The %s format does not match using a regular expression, it just scans non-whitespace characters. Even if it worked as you assumed, your regular expression would not be able to handle all JSON strings correctly (which might not be a problem if your input data format is sufficiently restricted, but would be unclean nonetheless).
Use a proper JSON parser. It's not really complicated. I used cJSON for a moderately complex case, you should be able to integrate it within a few hours.
To fix your immediate problem, use this format specifier:
"[\"%[^\"]s\",%f,%f,%f],"
The right syntax for parsing a set is %[...]s instead of %s[...].
That being said, sscanf() is not the right tool for parsing JSON. Even the "fixed" code would fail to parse strings that contain escaped quotes, for instance.
Use a proper JSON parser library.

Read string which contains \" from mongodb

In my MongoDB, I have stored below string
"description" : "25\"",
But when I try to read it in C++ driver using both ways below, I always get "25""
d->description=record.getStringField("description");
or
d->description = record.getField("description").jsonString(Strict);
I need to keep back slash \ here, because the string will be sent to web browser, JavaScript code will parse this string to JSON object.
Any way to do this?
Not sure how that string got in there, but this will not serialize or deserialize properly without the proper escaping. It should look more like this:
{ "description" : "25\\\"" }
You should update these with your driver, which should do the serialization properly just based on your regular input, ie 25".
When the fields in the document look like above then they will deserialize how you want.