Reputation: 2167
I need to strip the double quotes from a complex Json structure (object with children object and arrays in the hierarchy). I'm writing in Java (the app will run on Android).
I already have the string produced from a Json lib (Gson) and I am trying to strip the double quotes using regex instead of deserializing it back to an object and then serializing it without the double quotes in the key names.
I cannot find the right pattern or patterns to replace them.
example Json:
{
"key1":"value1",
"key2":"555f9ecd-2ced-42b7-b956-33e759bf8db1",
"key3":false,
"key4_parent":{
"child_key5":"Android",
"id":"7ae2facc-cdb3-7ae2-facccdb3bb86",
"name":"danny",
"bypassed":false
},
"object1":{
"deviceType":"Android",
"id":"7ae2facc-cdb3-7ae2-facccdb3bb86",
"base64val":"MENPSENJTFpZQzR3b1lvU1pQZTk=",
"name":"john"
},
"requiredLevel":"PUSH",
"level":"debug",
"status":1012,
"description":"Authentication in progress",
"objects":[
{
"deviceType":"Android",
"id":"7ae2facc-cdb3-7ae2-facccdb3bb86",
"base64val":"MENPSENJTFpZQzR3b1lvU1pQZTk=",
"name":"john"
},
{
"deviceType":"Android",
"id":"7ae2facc-cdb3-7ae2-facccdb3bb86",
"base64val":"MENPSENJTFpZQzR3b1lvU1pQZTk=",
"name":"john"
}
]
}
expected output :
{
key1:"value1",
key2:"555f9ecd-2ced-42b7-b956-33e759bf8db1",
key3:false,
key4_parent:{
child_key5:"Android",
id:"7ae2facc-cdb3-7ae2-facccdb3bb86",
name:"danny",
bypassed:false
},
object1:{
deviceType:"Android",
id:"7ae2facc-cdb3-7ae2-facccdb3bb86",
base64val:"MENPSENJTFpZQzR3b1lvU1pQZTk=",
name:"john"
},
requiredLevel:"PUSH",
level:"debug",
status:1012,
description:"Authentication in progress",
objects:[
{
deviceType:"Android",
id:"7ae2facc-cdb3-7ae2-facccdb3bb86",
base64val:"MENPSENJTFpZQzR3b1lvU1pQZTk=",
name:"john"
},
{
deviceType:"Android",
id:"7ae2facc-cdb3-7ae2-facccdb3bb86",
base64val:"MENPSENJTFpZQzR3b1lvU1pQZTk=",
name:"john"
}
]
}
I know that the output would not be a valid json. it's intended for easily searchable logging purposes.
any help would be greatly appreciated!
Upvotes: 9
Views: 8333
Reputation: 69
Above, @toto's method is quite concise and precise. I can write it in detail this way:
text.replaceAll("\"([^\"]+)\":", "$1:")
Or you can try a more visual way like this:
String regex = "\"[a-z_]+\":";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
String keyField = matcher.group(0);
String keyFieldWithoutQuotes = keyField.replace("\"", "");
text = text.replaceFirst(keyField, keyFieldWithoutQuotes);
}
System.out.println(text);
This way it will find key fields containing quotes one by one, then delete the quotes.
Upvotes: 0
Reputation: 41
javascript For those who come looking for a javascript answer and who need compatibility with all valid JSON including keys containing escaped quotes (\"), colons (:), or whitespace (\s), here are 2 options.
.replace(/("(\\[^]|[^\\"])*"(?!\s*:))|"((\\[^]|[^\\"])*)"(?=\s*:)/g, '$1$3')
OR
function unquoteKeys(json) {
return json.replace(/"(\\[^]|[^\\"])*"\s*:?/g, function (match) {
if (/:$/.test(match)) return match.replace(/^"|"(?=\s*:$)/g, '');
else return match;
}
}
This type of function can be used to do almost anything with valid JSON.
function modifyJson(json) {
return json.replace(/"(\\[^]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(\.\d+)?([eE][-+]?\d+)?/g, function (match) {
if (/^"/.test(match)) {
if (/:$/.test(match)) {
// match = Key with enclosing quotes & following colon (".*"\s*:)
} else {
// match = String (not key) with enclosing quotes (".*")
}
} else if (/true|false/.test(match)) {
// match = Boolean value (true|false)
} else if (/null/.test(match)) {
// match = Null value (null)
} else {
// match = Number (-?\d+(\.\d+)?([eE][-+]?\d+)?)
}
});
}
Upvotes: 4
Reputation: 1152
Could you try it?
jsonStr.replaceAll("\"(\\w+)\":", "$1:"));
Regex remove quotes from keys
Upvotes: 2
Reputation: 7166
Assuming there is no colon (:
) in the jsonstr, except as key-value separator, you can experiment with lookaheads ((?=...)
constructs) too:
jsonStr.replaceAll("\"(?=.*:)", "");
Upvotes: 0
Reputation: 4714
You use find by regex:
\"(\w+)\"\:
and replace with
$1:
In any editor
and in java will be
String res = str.replaceAll("\\\"(\\w+)\\\"\\:","\\\\$1:")
Upvotes: 0