Use regex in Powershell v2 to get values from a json file -


how access following values using regex function in powershell, , assign each 1 individual variable?:

id (i.e. value: token_id) - under token

id (i.e. value: tenant_id) - under token, tenant

adminurl (i.e. value: http://10.100.0.222:35357/v2.0) - first value under servicecatalog,endpoints

as using powershell v2, can't use convertfrom-json cmdlet. far i've tried converting document xml file using third-party ps script, doesn't right. i'd use regex, not comfortable it.

$json =     "{         "access": {             "metadata": {                 "is_admin": 0,                 "roles": [                     "9fe2ff9ee4384b1894a90878d3e92bab"                 ]             },             "servicecatalog": [                 {                     "endpoints": [                         {                             "adminurl": "http://10.100.0.222:8774/v2/tenant_id",                             "id": "0eb78b6d3f644438aea327d9c57b7b5a",                             "internalurl": "http://10.100.0.222:8774/v2/tenant_id",                             "publicurl": "http://8.21.28.222:8774/v2/tenant_id",                             "region": "regionone"                         }                     ],                     "endpoints_links": [],                     "name": "nova",                     "type": "compute"                 },                 {                     "endpoints": [                         {                             "adminurl": "http://10.100.0.222:9696/",                             "id": "3f4b6015a2f9481481ca03dace8acf32",                             "internalurl": "http://10.100.0.222:9696/",                             "publicurl": "http://8.21.28.222:9696/",                             "region": "regionone"                         }                     ],                     "endpoints_links": [],                     "name": "neutron",                     "type": "network"                 },                 {                     "endpoints": [                         {                             "adminurl": "http://10.100.0.222:8776/v2/tenant_id",                             "id": "16f6416588f64946bdcdf4a431a8f252",                             "internalurl": "http://10.100.0.222:8776/v2/tenant_id",                             "publicurl": "http://8.21.28.222:8776/v2/tenant_id",                             "region": "regionone"                         }                     ],                     "endpoints_links": [],                     "name": "cinder_v2",                     "type": "volumev2"                 },                 {                     "endpoints": [                         {                             "adminurl": "http://10.100.0.222:8779/v1.0/tenant_id",                             "id": "be48765ae31e425cb06036b1ebab694a",                             "internalurl": "http://10.100.0.222:8779/v1.0/tenant_id",                             "publicurl": "http://8.21.28.222:8779/v1.0/tenant_id",                             "region": "regionone"                         }                     ],                     "endpoints_links": [],                     "name": "trove",                     "type": "database"                 },                 {                     "endpoints": [                         {                             "adminurl": "http://10.100.0.222:9292",                             "id": "1adfcb5414304f3596fb81edb2dfb514",                             "internalurl": "http://10.100.0.222:9292",                             "publicurl": "http://8.21.28.222:9292",                             "region": "regionone"                         }                     ],                     "endpoints_links": [],                     "name": "glance",                     "type": "image"                 },                 {                     "endpoints": [                         {                             "adminurl": "http://10.100.0.222:8777",                             "id": "350f3b91d73f4b3ab8a061c94ac31fbb",                             "internalurl": "http://10.100.0.222:8777",                             "publicurl": "http://8.21.28.222:8777",                             "region": "regionone"                         }                     ],                     "endpoints_links": [],                     "name": "ceilometer",                     "type": "metering"                 },                 {                     "endpoints": [                         {                             "adminurl": "http://10.100.0.222:8000/v1/",                             "id": "2198b0d32a604e75a5cc1e13276a813d",                             "internalurl": "http://10.100.0.222:8000/v1/",                             "publicurl": "http://8.21.28.222:8000/v1/",                             "region": "regionone"                         }                     ],                     "endpoints_links": [],                     "name": "heat-cfn",                     "type": "cloudformation"                 },                 {                     "endpoints": [                         {                             "adminurl": "http://10.100.0.222:8776/v1/tenant_id",                             "id": "7c193c4683d849ca8e8db493722a4d8c",                             "internalurl": "http://10.100.0.222:8776/v1/tenant_id",                             "publicurl": "http://8.21.28.222:8776/v1/tenant_id",                             "region": "regionone"                         }                     ],                     "endpoints_links": [],                     "name": "cinder",                     "type": "volume"                 },                 {                     "endpoints": [                         {                             "adminurl": "http://10.100.0.222:8773/services/admin",                             "id": "11fac8254be74d7d906110f0069e5748",                             "internalurl": "http://10.100.0.222:8773/services/cloud",                             "publicurl": "http://8.21.28.222:8773/services/cloud",                             "region": "regionone"                         }                     ],                     "endpoints_links": [],                     "name": "nova_ec2",                     "type": "ec2"                 },                 {                     "endpoints": [                         {                             "adminurl": "http://10.100.0.222:8004/v1/tenant_id",                             "id": "38fa4f9afce34d4ca0f5e0f90fd758dd",                             "internalurl": "http://10.100.0.222:8004/v1/tenant_id",                             "publicurl": "http://8.21.28.222:8004/v1/tenant_id",                             "region": "regionone"                         }                     ],                     "endpoints_links": [],                     "name": "heat",                     "type": "orchestration"                 },                 {                     "endpoints": [                         {                             "adminurl": "http://10.100.0.222:35357/v2.0",                             "id": "256cdf78ecb04051bf0f57ec11070222",                             "internalurl": "http://10.100.0.222:5000/v2.0",                             "publicurl": "http://8.21.28.222:5000/v2.0",                             "region": "regionone"                         }                     ],                     "endpoints_links": [],                     "name": "keystone",                     "type": "identity"                 }             ],             "token": {                 "audit_ids": [                     "gsjrnoqfsqeuluo0qejprq"                 ],                 "expires": "2014-12-15t15:09:29z",                 "id": "token_id",                 "issued_at": "2014-12-15t14:09:29.794527",                 "tenant": {                     "description": "auto created account",                     "enabled": true,                     "id": "tenant_id",                     "name": "username"                 }             },             "user": {                 "id": "user_id",                 "name": "username",                 "roles": [                     {                         "name": "_member_"                     }                 ],                 "roles_links": [],                 "username": "username"             }         }     }" 

if using .net 3.5 or higher on machines powershell 2.0, you can use json serializer (from linked answer):

[system.reflection.assembly]::loadwithpartialname("system.web.extensions") $json = "{a:1,b:2,c:{nested:true}}" $ser = new-object system.web.script.serialization.javascriptserializer $obj = $ser.deserializeobject($json) 

this preferable using regex.

for admin url example, you'd refer to:

$obj.access.servicecatalog[0].endpoints[0].adminurl 

using regex anyway

if ($json -match '(?s)"servicecatalog".+?"endpoints".+?"adminurl"[^"]+"(?<adminurl>[^"]+)".+?"token".+?"id"[^"]+"(?<tokenid>[^"]+)".+?"tenant".+?"id"[^"]+"(?<tenantid>[^"]+)') {     $matches['adminurl']     $matches['tokenid']     $matches['tenantid'] } 

regex breakdown:

  • (?s) tells regex engine . matches anything, including newlines (by default wouldn't).
  • of course of "whatever" parts match literally.
  • .+? matches 1 or more of character (including newlines since we're using s), , ? makes non-greedy.
  • [^"]+ matches 1 or more characters not double quote.
  • () capturing group. using (?<name>) can refer group later name rather number, nicety.

so basic idea literals, point can capture values needed. after -regex operator match in powershell, $matches variable populated matches, groups, etc.

note relies on values being in order in posted json. if in different order fail.

to work around split 3 different regex matches.


Comments