Regular Expression to extract string from JSON response - json

I am trying to extract the value from timeZoneId but regexr says no match.
here is the JSON code:
{
"dstOffset": 0,
"rawOffset": 25200,
"status": "OK",
"timeZoneId": "Asia/Jakarta",
"timeZoneName": "Western Indonesia Time"
}
and when I test on regexplanet its says Illegal repetition {\s*"timeZoneId"\s*:\s*(.+?)\s*\}
Can anyone help and explain what i'm doing wrong.
UPDATE
I am parsing the result in a while loop, so I only need the RegEx for the line when it match this "timeZoneId": "Asia/Jakarta", I want to grab out the Asia/Jakarta.
Thanks
G

The { and } brackets are special characters in regex and need to be escaped. See more here: PatternSyntaxException: Illegal Repetition when using regex in Java
Also, as dustmouse said, you are requiring {} around the timeZoneId but there isn't just whitespace between the { and the "timeZoneId". All your other fields are there which is causing it not to match.

There's a lot more than just whitespace between the opening brace and "timeZoneId".
You want: /^\s*"timeZoneId"\s*:\s*(.+?)(?=$|,)/m
But, use a JSON parser. For example jq
echo '{
"dstOffset": 0,
"rawOffset": 25200,
"status": "OK",
"timeZoneId": "Asia/Jakarta",
"timeZoneName": "Western Indonesia Time"
}' | jq -r '.timeZoneId'
Asia/Jakarta

Related

Processing JSON arrays

I have a JSON file arranged in this pattern:
[
{
"Title ID": "4224031",
"Overtime Status": "Non-Exempt",
"Shift rates": "No Shift rates",
"On call rates": "No On call rates"
},
[
{
"Step: 1.0": [
"$38.87",
"(38.870000)"
]
}
]
][
{
"Title ID": "4225031",
"Overtime Status": "Non-Exempt",
"Shift rates": "No Shift rates",
"On call rates": "No On call rates"
},
[
{
"Step: 1.0": [
"$38.87",
"(38.870000)"
]
}
]
]
I am trying to get it into a Pandas DataFrame. I have tried opening a connection to the JSON file and running JSON.load(s). Unfortunately, I get JSON decode errors like: "JSONDecodeError: Extra data: line 16 column 2 (char 182)". When running the JSON through a linter, I see that there might be an issue with the way the JSON is presented in the file. The parts between the brackets are valid but when wrapped in brackets, become invalid. I have then tried to get at the dictionaries with the wrapping brackets but have not been able to make much progress. Does anyone have tips on how I can successfully access this JSON data and get it into a pandas DataFrame?
The json is invalid beacuase it has more than one root in this representation.
This has to be like this
jsonObject = [{"1":"3"}], [{"4":"5"}]
Hacks that I am able to think of are replace these brackets ][ to this ],[ by find and replace in editor. You'll be able to then create a dataframe as its a list now.
Second, if its not a one time job, then you need to write a regex that can do this for you in text cleaning pipeline(or code). I'm not good at writing of working regex(sorry mate).
I found a solution.
First, after examining the JSON data in a linter, I found that I had some extra brackets and braces at different points. So, I am running the data through a regex that cleans out the unnecessary brackets and braces.
Next, I run each line, which now looks like a string dictionary through json.loads
Finally, I call pd.DataFrame(pd.json_normalize(data)) to get my desired pandas dataframe.
Thanks for the help from commenters.

NiFi expression language dealing with special characters in JSON keys

So I have some json in which the keys might be something like this:
{
"name" : "John",
"num:itparams:enterprise:2.0:content" : {
"housing" : "5"
},
"num rooms": "12"
}
I get this json from an http request, and I need to use the evaluateJsonPath processor to create attributes from them.
name is easy, i just use $.name
But how would I access the other two? I imagine you would put them in quotes somehow to escape the special characters but just doing $."num:itparams:enterprise:2.0:content" doesnt work.
You can use the bracket for the key-value which has the special characters such as
$.['num:itparams:enterprise:2.0:content'].housing
then it will give you the evaluated result 5.

How to use jq to get a value of decimal/number type from a JSON response which is not surrounded by " "

I am new to shell scripting and I need some help.
I am trying to use jq to get values from a api response and check for its correctness.
Here is a sample for how the response looks like,
{
"data" : {
"transactionType" : "Sales",
"transactionSubType" : "DomesticSale",
"Items" : [ {
"itemID" : "2",
"itemType" : "Good",
"amount" : 5.0,
"tax" : 1.0
} ]
}
}
I am able to get the values for transactionType or transactionsubtype or even ItemID values etc as given below
jq '.data.transactionType'
jq '.data.Items[0].itemID'
for Transaction type and item id
but when it comes to values of numeric types i.e., without the quotes in it, I don't get any value.
I am using similar syntax for the numeric type also as shown below.
jq '.data.Items[0].amount'
jq '.data.Items[0].tax'
Please help!!!
Your jq invocations are fine, but the sample data is missing a final closing brace ("}"), so perhaps you were not feeding jq properly.
If you're wondering why you didn't see an error message, it's almost certainly because jq 1.5 is not very good about handling incomplete JSON. The problem has since been fixed at "master". With the current version, you'd see something like this:
parse error: Unfinished JSON term at EOF at line 15, column 0

Regex Get Rid of Hyphens Between Quotes

I get a json string back from a device, the string has hyphens in the defintion names and I want to remove that...
Current I have
\"(.+?)\":
But that gets everything within "": I want only the hyphen in there, not all the text. I know I am close just having trouble because regex always confuses me. Below I would like to correct serial-number to serialnumber but not value-2....help!
{
"result": {
"Response": {
"info": {
"serial-number": "xyz",
"value1": "value-2",
You can match
/"([^"]*)-([^"]*)":/
And then replace with just the two submatches.
See http://www.regexpal.com/?fam=95143
This is the sort of js code that should work:
json = json.replace(/"([^"]*)-([^"]*)":/g,'"$1$2"');
Here's another version, using a lookahead:
json = json.replace(/-(?=[^"]*":)/g,"");
This assumes there's never a space between the closing " and the :

query with filter in JsonPath

I have a structure like this:
{
title: [
{lang: “en”, value: “snickers”},
{lang: “ru”, value: “сниккерс”}
]
}
I need to take a result such this:
“snickers”
I finished with query:
$.title[?(#.lang="en")].value
but it doesn't work
JsonPath always returns an array of results, so you will not be able to get a single value such as "snickers" on its own. You will be able to get ["snickers"] though. To get it working you need to use a double equal sign instead of single:
$.title[?(#.lang=="en")].value
Also note that another reason you may be having issues is that your double quotes in the json are not the standard double quote characters. Depending on how you consume the json you may also need to wrap the property names in double quotes. The json standard is quite strict, and if you are parsing your json from text it has to follow these rules. Here is version of the json corrected in this way:
{
"title": [
{"lang": "en", "value": "snickers"},
{"lang": "ru", "value": "сниккерс"}
]
}
I have tested the above query with the corrected json using http://www.jsonquerytool.com.