Home > Enterprise >  Regex to remove the access token pattern and replace with empty string
Regex to remove the access token pattern and replace with empty string

Time:07-15

I am trying to find and remove the access token pattern from JSON response.

{
"name":"any name",
"access_token":"xxx-xxxxx-1234-45rf",
"some_data":"some random data",
"Api_key":"1234-cd34-xxx-xxxx"
}

I am trying with below regex

".*-.*-.*"

but it is removing the entire line from the string, e.g.

"access_token":"xxx-xxxxx-1234-45rf",

from the JSON.

The result I want to achieve is as below.

{
"name":"any name",
"access_token":"",
"some_data":"some random data",
"Api_key":""
}

The above access_token is just a dummy and the actual key can be of different lengths of string with hyphen.

CodePudding user response:

You can make use of regex inside replaceAll function. This will replace acces_token and its value with an empty string.

String jsonString = "{ \"name\":\"any name\", \"access_token\":\"xxx-xxxxx-1234-45rf\", \"some_data\":\"some random data\", \"Api_key\":\"1234-cd34-xxx-xxxx\" }";

jsonString.replaceAll("(\"access_token\":)(\")(.*?)(\",) ", "")

CodePudding user response:

Try this:

String json = "{ \"name\":\"any name\", \"access_token\":\"xxx-xxxxx-1234-45rf\", \"some_data\":\"some random data\", \"Api_key\":\"1234-cd34-xxx-xxxx\" }";

json = json.replaceAll(".{3}-.{5}-[\\d]{4}-[\\w]{4}", "");

See it working here

CodePudding user response:

The code you need is:

String json = "{ \"name\":\"any name\", \"access_token\":\"xxx-xxxxx-1234-45rf\", \"some_data\":\"some random data\", \"Api_key\":\"1234-cd34-xxx-xxxx\" }";
json = json.replaceAll("(\"access_token\")\\s*:\\s*\"[^\"] \"", "$1:\"\"");

The regex picks up the quoted item name (within the parenthesis, therefore it gets stored as group 1), followed by some optional space characters, a colon, some more optional space characters and whatever non-empty quoted value that follows. All this gets replaced by the quoted item name (coming from group 1!) followed by a colon and an empty quoted value.

CodePudding user response:

JAVA

import required packages

import java.util.regex.Matcher;
import java.util.regex.Pattern;
❶ json string
public static void main(String[] args) {
    Pattern p = Pattern.compile(":\\\"(.*?)\\\"");
    String json = "{\"name\":\"any name\", \"access_token\":\"xxx-xxxxx-1234-45rf\", \"some_data\":\"some random data\", \"Api_key\":\"1234-cd34-xxx-xxxx\"}";
    Matcher m = p.matcher(json);
    StringBuffer buf = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(buf, String.format(":\"%s\"", m.group(1)).replaceAll(".*-.*-.*", ":\"\""));
    }
    m.appendTail(buf);
    System.out.println(buf); // output: {"name":"any name", "access_token":"", "some_data":"some random data", "Api_key":""}
}
❷ static method
private static String removeToken(String content) {
    Pattern p = Pattern.compile(".*-.*-.*");
    return p.matcher(content).replaceAll("");
}

JavaScript

❶ no nested object

const obj = {
  "name": "any name",
  "access_token": "xxx-xxxxx-1234-45rf",
  "some_data": "some random data",
  "Api_key": "1234-cd34-xxx-xxxx"
};

for (const [key, value] of Object.entries(obj)) {
  obj[key] = value.replace(/.*-.*-.*/, '');
}

console.log(obj);

❷ multi-level nested object

const obj = {
  "name": "any name",
  "access_token": "xxx-xxxxx-1234-45rf",
  "some_data": "some random data",
  "Api_key": "1234-cd34-xxx-xxxx",
  "obj": {
    "111name": "any name",
    "access_token": "xxx-xxxxx-1234-45rf",
    "some_data": "some random data",
    "Api_key": "1234-cd34-xxx-xxxx"
  }
};

const restructure = obj => {
  return Object.keys(obj).reduce((data, key) => {
    let value = obj[key];
    if (typeof value === 'string') {
      value = value.replace(/.*-.*-.*/, '');
    } else if (typeof value === 'object') {
      value = restructure(value)
    }
    data[key] = value;
    return data
  }, {})
}

console.log(restructure(obj));

  • Related