Home > Back-end >  find string in json file using regular expression
find string in json file using regular expression

Time:12-09

I have such a code in json file and need to find password from special array using regular expression. For example, i want to find the password from sftpConfig not pbx

"pbx": {
    "dbConfig": {
        "username": "postgres",
        "password": "postgres",
        "database": "autocaller",
        "host": "postgres",
        "dialect": "postgres",
        "operatorsAliases": false,
        "dialectOptions": {
    "connectTimeout": 60000,
            "options": {
                "requestTimeout": 3000
            }
        },
        "pool": {
            "max": 10,
            "min": 0,
            "idle": 10000
        }
    }
},
"strapi": {
    "credentials": {
        "identifier": "admin",
        "password": "admin"
    }
},

"sftpConfig":{ "host": "1.1.1.1", "port": "22", "username": "admin", "password": "admin1" }

CodePudding user response:

You can try the following pattern:

/sftpConfig": \{[^\}]*"password": "([^"]*)"/

Or python3:

import json

json_data = json.loads(json_string)

# Access the value of the "password" key using dot notation
password1 = json_data.pbx.dbConfig.password
password2 = json_data.strapi.credentials.password

print(password1)
print(password2)

I hope that help.

  •  Tags:  
  • json
  • Related