json解析提取子字符串

问题描述 投票:-2回答:2

我试图从云代工厂env json字符串中提取凭证客户端密钥

cf env myapp

给出了确切的跟随,(不是一个合适的json所以这就是为什么我不能使用jq)

Getting env variables for app icm in org myorg / space myspace as 
xxyy...
OK


{
  "myenv_env_json": {
"http_proxy": "http://mycompany-svr-proxy-qa.mycompany.com:7070",
"https_proxy": "http://mycompany-svr-proxy-qa.mycompany.com:7070",
"no_proxy": "*.mycompany.com"
  },
  "running_env_json": {},
  "system_env_json": {
"VCAP_SERVICES": {
  "user-provided": [
    {
      "name": "myapp-parameters",
      "instance_name": "myapp-parameters",
      "binding_name": null,
      "credentials": {
        "auth-domain": "https://sso.login.run-np.mycompany.com",
        "backend-url-other": "https://myservice-other.apps-np.mycompany.com",
        "client-secret": "121322332-32322-23232-232-32-23232",
        "stage": "mystg",
        "backend-url": "https://myservice-other.apps-np.mycompany.com",
        "client-secret-other": "121322332-32322-23232-232-32-23232"
      },
      "syslog_drain_url": "",
      "volume_mounts": [],
      "label": "user-provided",
      "tags": []
    },
    {
      "name": "appdynamics",
      "instance_name": "appdynamics",
      "binding_name": null,
      "credentials": {
        "account-access-key": "1213232-232-322-2322323-2323232-311",
        "account-name": "customer1",
        "application-name": "myenv-dev",
        "host-name": "appdx-qa.mycompany.com",
        "node-name": "$(ruby -e \"require 'json'; a = JSON.parse(ENV['VCAP_APPLICATION']); puts \\\"#{a['application_name']}-#{a['cf_api'].split(/\\.|-/)[2]}:#{a['instance_index']}\\\"\")",
        "port": "9401",
        "ssl-enabled": "true",
        "tier-name": "$(ruby -e \"require 'json'; a = JSON.parse(ENV['VCAP_APPLICATION']); puts \\\"#{a['application_name']}-#{a['cf_api'].split(/\\.|-/)[2]}\\\"\")",
        "version": "4.2.7_1"
      },
      "syslog_drain_url": "",
      "volume_mounts": [],
      "label": "user-provided",
      "tags": []
    }
  ],
  "p-identity": [
    {
      "name": "sso",
      "instance_name": "sso",
      "binding_name": null,
      "credentials": {
        "auth_domain": "https://sso.login.run-np.mycompany.com",
        "client_secret": "123232-23232-2323243-242323r3r",
        "client_id": "afdvdf-dvdfdd-fgdgdf-d23232"
      },
      "syslog_drain_url": null,
      "volume_mounts": [],
      "label": "p-identity",
      "provider": null,
      "plan": "sso",
      "tags": []
    }
  ]
}
 },
 "application_env_json": {
 "VCAP_APPLICATION": {
  "cf_api": "https://api.run-np.mycompany.com",
  "limits": {
    "fds": 16384
  },
  "application_name": "myapp",
  "application_uris": [
    "myapp-dev.apps-np.mycompany.com"
  ],
  "name": "myapp",
  "space_name": "myapp-dev",
  "space_id": "392929-23223-2323-2322-2322",
  "uris": [
    "myapp-dev.apps-np.mycompany.com"
  ],
  "users": null,
  "application_id": "fwew78cc-wewc5c-dfd8a7-89d5-fdfefwewb"
}
 }
 }

 User-Provided:
 APP_ENV: development
 GRANT_TYPE: authorization_code
 SSO_AUTO_APPROVED_SCOPES: openid
 SSO_IDENTITY_PROVIDERS: mycompany-single-signon
 SSO_LAUNCH_URL: https://myapp-dev.apps-np.mycompany.com/
 SSO_REDIRECT_URIS: https://myapp-dev.apps-np.mycompany.com/callback,http://myapp-dev.apps-np.mycompany.com/callback
 SSO_SCOPES: openid,user_attributes
 callback_url: https://myapp-dev.apps-np.mycompany.com/callback
 client_secret: secret
 client_secret_other: secretother

 No running env variables have been set

 Staging Environment Variable Groups:
 http_proxy: http://myapp-svr-proxy-qa.mycompany.com:7070
 https_proxy: http://myapp-svr-proxy-qa.mycompany.com:7070
 no_proxy: *.mycompany.com

这是我想要使用的,到目前为止没有运气提取p-identity sub json,我的sed有什么问题

 cf env myapp|sed 's/.*\(p-identity[^}].*}\).*/\1/p'

我的预期输出应如下

"p-identity": [
    {
      "name": "sso",
      "instance_name": "sso",
      "binding_name": null,
      "credentials": {
        "auth_domain": "https://sso.login.run-np.mycompany.com",
        "client_secret": "123232-23232-2323243-242323r3r",
        "client_id": "afdvdf-dvdfdd-fgdgdf-d23232"
      }
regex sed pivotal-cloud-foundry
2个回答
0
投票

对于您的情况,可能更容易将输出通过管道输出到grep以提取JSON,然后使用jq提取所需的字段,例如:

cf env myapp | grep -oz'{。*}'| jq'你的过滤器'


0
投票

我发现了一个肮脏的解决方法,可能效率不高但现在有效

cf env myapp|sed 1,4d|sed -n '/User-Provided:/q;p'|jq -c -r '.VCAP_SERVICES."p-identity"[0].credentials.client_secret'| head -n1
© www.soinside.com 2019 - 2024. All rights reserved.