0

Requirements:

For each Json field where key matches specified constant replace value with another constant.

{"regular":"a", "sensitive":"b"}

Parameters "sensitive", "*****". Expected:

{"regular":"a", "sensitive":"*****"}

Values may, or may not have double quotes around them. Replacement constant is double quouted always. Json may be malformed. Java implementation preferably.

Key comparison is case insensitive.

3
  • 2
    Not a good idea to manipulate JSON using regex. Use some JSON library Commented Mar 7, 2018 at 7:19
  • @TheLostMind I have a requirement that Json may be malformed. Commented Mar 7, 2018 at 7:22
  • hmm. Well, in that case, there are ways to achieve this. Even though I've added an answer to this, I strongly recommend avoiding it Commented Mar 7, 2018 at 7:33

3 Answers 3

4

Depending on how malformed your "JSON" is, the following might work - if not, we need more test cases:

"sensitive"\s*:\s*  # match "sensitive":
(                   # capture in group 1:
 "[^"]*"            # any quoted value
|                   # or
 [^\s,{}"]*         # any unquoted value, ending at a comma, brace or whitespace
)                   # end of group 1

In Java:

String resultString = subjectString.replaceAll(
    "(?x)\"sensitive\"\\s*:\\s* # match \"sensitive\":\n" +
    "(                          # capture in group 1:\n" +
    " \"[^\"]*\"                # any quoted value\n" +
    "|                          # or\n" +
    " [^\\s,{}\"]*              # an unquoted value, ending at comma, brace or whitespace\n" +
    ")                          # end of group 1",
    "\"sensitive\":\"******\"");

Test it live on regex101.com.

Sign up to request clarification or add additional context in comments.

Comments

3

You can use positive lookbehind to achieve this :

public static void main(String[] args) {
    String s = "{\"regular\":\"a\", \"sensitive\":\"b\"}";
    String key = "sensitive";
    String val = "****";
    System.out.println(s.replaceAll("(?<=\"" + key + "\":\")(\\w+)", val));
     key = "regular";
    System.out.println(s.replaceAll("(?<=\"" + key + "\":\")(\\w+)", val));

}

O/P :

{"regular":"a", "sensitive":"****"}
{"regular":"****", "sensitive":"b"}

3 Comments

how can I make key comparison case-insenstive?
used (?i) for case insensitive matching
what would you do if "sensitive" were an object? {sensitive:{x:y} and you wanted to wipe the object and put in the string?
1

You can use the following regex:

String t= "{\"regular\":\"a\", \"sensitive\":\"b\"}"; //{"regular":"a", "sensitive":"b"}
String r = t.replaceAll("(\\s*)\"?sensitive\"?\\s*:\\s*\"?b\"?\\s*", "$1\"sensitive\":\"*****\""); 
System.out.println("output "+r); //output {"regular":"a", "sensitive":"*****"}

t= "{\"regular\":\"a\",sensitive:b}"; //{"regular":"a", "sensitive":"b"}
r = t.replaceAll("(\\s*)\"?sensitive\"?\\s*:\\s*\"?b\"?\\s*", "$1\"sensitive\":\"*****\""); 
System.out.println("output "+r); //output {"regular":"a","sensitive":"*****"}

DEMO: https://regex101.com/r/uHUhEl/1/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.