I have a string data which has some unwanted text. I want to delete that data from string using a regular expression. The example data is given below from which I want to eliminate the key meth and its corresponding data. The regular expression I created for this purpose is "meth(S+)([\\},])" but its not working.
I used the following code to use this regular expression:
json = json.replaceAll("meth(S+)([\\},])","");
Below is the string data that is present in json :
{
data: [
{
"city": "barcelona",
"Date": "4 Apr 2014",
"Name": "A-B",
"meth": function(){
return_LANG=="en-us"?"A-T": "A-T "
},
"fo": "null",
},
{
"city": "Newyork",
"Date": "4 Apr 2014",
"Name": "B-C",
"meth": function(){
return_LANG=="en-us"?"S-E": "शक्तिपुंजएक्स."
},
"fo": "null",
}
]
}
The result I am getting is the same string as my regular expression is unable to find the matching data in String.
Please help me correcting my regex.
This should do the trick:
json = json.replaceAll("(?s)meth.*?\\},", "");
Explanation:
(?s) is for multiline regular expressions. The dot . will then also match new line characters (equal to the flag DOTALL, see Pattern#DOTALL).
.*? searches any letter in a non greedy way till it finds the },
try this
s = s.replaceAll("(?s)\"meth\":.*?},\\s+","");
Related
I have been trying to replace the dots with a different symbol using regex. But unable to add all the dots in capturing groups to replace. Basically this dots needs to be replaced only for keys.
I have tried with [\w\d]*([.])]* which gets me all the instances where dots exists with words. But unable to set it just for keys.
If at all try ending the expression with " and : like [\w\d]+?([.])]*?[\w\d]+?[":] it selects only the last part of the key or value.
{
"header": {
"dataModelVersion": "3"
},
"content": {
"sasl.kerberos.service.name": {
"displayName": "Kerberos Service Name",
"identifiesControllerService": false,
"name": "sasl.kerberos.service.name",
"sensitive": false
}
}
}
From your input json, here I have assume that there is no digit present on json key. So, there is no need to add \d on regex.
Here, I have replace dot (.) on json key with character underscore (_). Yes, you can replace with any character if you want.
Matcher matcher = Pattern.compile("\"([\\w\\.]*)\"[:]", Pattern.CASE_INSENSITIVE)
.matcher(json);
while(matcher.find()) {
if(matcher.group().contains(".")) {
json = json.substring(0, matcher.start())
+ matcher.group().replace(".", "_")
+ json.substring(matcher.end());
}
}
System.out.println(json);
Result of this program as below:
{
"header": {
"dataModelVersion": "3"
},
"content": {
"sasl_kerberos_service_name": {
"displayName": "Kerberos Service Name",
"identifiesControllerService": false,
"name": "sasl.kerberos.service.name",
"sensitive": false
}
}
}
I've got a chunk of JSON that looks like this:
{"id": "2345", "in": "sadfasd647", "name": "Delta", "created": "2018-01-19", "updated": ""}
Unfortunately, the service I'm sending this too doesn't handle blank entries. I have limited control over the source, so I'm looking for some Java Regex that I can use with ReplaceAll to get rid of all empty entries.
The result should look like:
{"id": "2345", "in": "sadfasd647", "name": "Delta", "created": "2018-01-19"}
The closest I've got is this:
string.replaceAll(",?\"*\":\\", "");
But I'm getting this error:
java.util.regex.PatternSyntaxException: Unexpected internal error near index 7
Try this regex, also the first value could be blank and should still be replaced correctly:
(, "\w+": "")|("\w+": "",)
So your Java Code should look like this:
String json = "{\"id\": \"2345\", \"in\": \"sadfasd647\", \"name\": \"Delta\", \"created\": \"2018-01-19\", \"updated\": \"\"}";
String result = json.replaceAll("(, \"\\w+\": \"\")|(\"\\w+\": \"\",)", "");
System.out.println(result);
Which prints the following result:
{"id": "2345", "in": "sadfasd647", "name": "Delta", "created": "2018-01-19"}
Try this (assuming the id is never blank):
String json = "{\"id\": \"2345\", \"in\": \"sadfasd647\", \"name\": \"Delta\", \"created\": \"2018-01-19\", \"updated\": \"\"}";
json = json.replaceAll(", \"\\w+?\": \"\"", "");
System.out.println(json);
Regex: , "\w+?": ""
Output: {"id": "2345", "in": "sadfasd647", "name": "Delta", "created": "2018-01-19"}
You are getting that exception in your regex because of \\ at the end. This would be equal to a single \ for the regex, so it expects a character to be escaped after it, while you have none.
Try this
Try replacing any empty string or whitespace preceded and followed by a comma (or by [ or ] if it occurs exactly at the beginning or end of the list) with null. It should work whether the actual JSON string has whitespace or not.
String result = str.replaceAll("(?<=,|\\[)\\s*(?=,|\\])","null");
I was using WOT(web of trust) api.In this, i am getting response in this format,
process( {
"www.google.com": {
"target": "google.com",
"0": [ 95, 84 ],
"1": [ 95, 84 ],
"2": [ 95, 84 ],
"4": [ 93, 78 ],
"categories": {
"501": 92
}
}
} )
I am very confused to extract the data string from this format of JSON. I searched a lot but could not find any way.
I want to extract from categories... means I want to save '502' value.
Problem : JSON Objects starts with "{" and ends with "}", the response you're getting is not a correct JSON Format.
Analysis : Why? you might ask, the response started with p char from process, and ends with ) char, instead of "}".
Solution : To fix the json, we will need to remove the process ( and ) string, so that the response can be parsed as JSON.
Hint : Use String.replace() method to replace the unwanted string with empty string "", more information here and here
Hope this helps, and Good Luck! ^^
Reid
I have got a valid complex json and I need to parse this json and print the values of only ak, dt and mi from this complex json in java... hope you can help me...
{
"CP": "{\"e\":{\"h\":{\"ak\":\"1c8d1d7eaa32ff3f58a882\",\"at\":\"app\"},\"c\":{\"dt\":\"MEmulator\",\"mi\":\"DD278047D56BF292F1FC16F\",\"ui\":\"m4J\/2s=\",\"av\":\"0.2\",\"pn\":\"WP\",\"pv\":\"7.10\",\"nv\":\"C# 1.1.0\",\"al\":\"en\"},\"b\":[{\"ts\":139658547,\"tz\":-400,\"s\":\"StartUpScreen\",\"et\":8,\"ev\":\"sessionStart\",\"si\":\"19477682-de55-414f-82c9-19bec331dc33\",\"tt\":{\"DaySessionStarted\":\"Tuesday\"}},{\"ts\":1319549658751,\"tz\":-400,\"s\":\"StartUpScreen\",\"et\":3,\"ev\":\"AutomaticFeedRefresh\",\"si\":\"19477682-de5ec331dc33\",\"tt\":{}},{\"ts\":1319549675609,\"tz\":-400,\"s\":\"MainScreen\",\"et\":3,\"ev\":\"MainScreen Event\",\"si\":\"19477682-de55-414f-82c9-19bec331dc33\",\"tt\":{}},{\"ts\":1319549677179,\"tz\":-400,\"s\":\"MainScreen\",\"et\":3,\"ev\":\"MainScreen Event\",\"si\":\"19477682-de55-414f-82c9-19bec331dc33\",\"tt\":{}},{\"ts\":1319549678401,\"tz\":-400,\"s\":\"MainScreen\",\"et\":3,\"ev\":\"MainScreen Event\",\"si\":\"19477682-de55-414f-82c9-19bec331dc33\",\"tt\":{}},{\"ts\":1319549679973,\"tz\":-400,\"s\":\"MainScreen\",\"et\":3,\"ev\":\"MainScreen Event\",\"si\":\"19477682-c9-19bec331dc33\",\"tt\":{}}],\"tt\":{\"OSV\":\"ME\"}}}",
"SP": {
"httpHeaders": {
"x-bluecoat-via": [
"35D3468F4D5F18"
],
"content-type": [
"application\/x-form-ur"
],
"connection": [
"Keep-Alive"
],
"host": [
"20.198.134.198:8080"
],
"accept": [
"text\/html, image\/gif, image\/jpeg, *; q=.2, *\/*; q=.2"
],
"content-length": [
"1791"
],
"user-agent": [
"Java\/1.6.0_23"
]
},
"senderIp": [
"112.101.216.113"
],
"receiveTimeStamp": "2012-06-26T06:29:36+0000"
}
}
Use json-path.
It's like xpath for JSON, and will allow you to write string queries on JSON objects.
There are a lot of examples on the project site showing possible usages, but in your case it's probably just a simple dot notation.
An example for the provided JSON:
// First extract the CP value, as its JSON-string-inside-JSON:
String cp = JsonPath.read(yourJsonString, "$.CP");
// Treat the `cp` as another JSON-string, and extract the ak value:
String ak = JsonPath.read(cp, "$.e.h.ak");
// Do the rest yourself...
You can use JsonPath to extract the value. I recommend JsonSurfer library.
<dependency>
<groupId>com.github.jsurfer</groupId>
<artifactId>jsurfer-simple</artifactId>
<version>1.2.1</version>
</dependency>
The sample code solving your problem in two steps:
1) Extract plain string from "$.CP" node.
2) Parse the "CP" string and extract value for "ak", "dt" and "mi".
JsonSurfer jsonSurfer = JsonSurfer.simple();
String cp = jsonSurfer.collectOne(new StringReader(yourString), String.class, "$.CP");
Collection<Object> result = jsonSurfer.collectAll(new StringReader(cp), "$..ak", "$..dt", "$..mi");
Like the others have suggested there are numerous libs out there that you can use (npe suggestion seems really nice). On the other hand, if you only have those simple cases and you don't really need to do anything else with JSON, maybe all you need is a regex. In essence JSON is just text, so you can do something like this:
Pattern akPattern = Pattern.compile("ak\":\"([^\"]+)");
Matcher matcher = akPattern.matcher(jsonAsString);
matcher.find();
String akValue = matcher.group(1);
System.out.println(akValue);
This prints out the value for "ak".
But again, I would only do this if I didn't have any other JSON requirements. Otherwise, go with a JSON lib.
My 2 cents.
I am using gson to create Java objects and everything works great until I get the following:
{
"SkuID": "2040",
"CheckDigit": "8",
"StockNumber": "2040-8",
"ProductName": "SalesReceiptBook(1)8"x4"(50)(3-PartNCR)",
"YourCost": "4.45",
"Points": "0.00",
"IsKosher": "False"
},
GSon determines the " before the 8 as the end of the value and this stops GSon from further parsing and I get an invalid JSON error.
Thank you!
Robbie
The basic answer is to encode it properly. See the string diagram (4th) on http://www.json.org/ for how you're allowed to encode, or alternatively, validate your json at http://jsonlint.com.
Your string should be
{
"SkuID": "2040",
"CheckDigit": "8",
"StockNumber": "2040-8",
"ProductName": "SalesReceiptBook(1)8\"x4\"(50)(3-PartNCR)",
"YourCost": "4.45",
"Points": "0.00",
"IsKosher": "False"
}
The JSON you posted isn't valid.
You need to escape the " character inside the ProductName string and you have a extra comma at the end.
{
"SkuID": "2040",
"CheckDigit": "8",
"StockNumber": "2040-8",
"ProductName": "SalesReceiptBook(1)8\"x4\"(50)(3-PartNCR)",
"YourCost": "4.45",
"Points": "0.00",
"IsKosher": "False"
}
In the future you can easily check if the JSON is valid using this online validator http://jsonlint.com/
Gson shouldn't do this by default, it should escape the " with a \ for you, If you datatype for ProductName is not a straight string this could be part of your problem when the object is serialized.