Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConvertFrom-Json PowerShell Cmdlet not parsing entire JSON object

I have this JSON in a file called test.txt

{
  "local-dev": {
    "client": {
      "server-url": "http://localhost:3000"
    },
    "server": {
      "renterEndpoint": {
        "rejectUnauthorized": false,
        "host": "blah.blah.com",
        "port": 443,
        "path": "/api/renter"
      },
      "homeownerEndpoint": {
        "rejectUnauthorized": false,
        "host": "blah.blah.com",
        "port": 443,
        "path": "/api/homeowner"
      }
    }
  }
}

When I run this PowerShell command:

Get-Content "test.txt" -Raw | ConvertFrom-Json

The out put I get does not include any of the objects under the second level (i.e. the client and server objects have no properties).

local-dev
---------
@{client=; server=}

Anyone have any ideas?

like image 754
dprothero Avatar asked Nov 24 '14 23:11

dprothero


1 Answers

The data you want is there. You just need to navigate the "nodes" ( dont know the proper terms ) If you return the data from your file into a variable and use Get-Member you can see what you are looking for.

PS C:\Users\Cameron> $json | Get-Member | Select-Object name

Name                                                                                                                                                                                              
----                                                                                                                                                                                              
Equals                                                                                                                                                                                            
GetHashCode                                                                                                                                                                                       
GetType                                                                                                                                                                                           
ToString                                                                                                                                                                                          
local-dev     

Lets see whats in local-dev. note the quotes around the property. Need PowerShell to treat this as a string else you will get parsing errors.

PS C:\Users\Cameron> $json."local-dev"

client                                        server                                       
------                                        ------                                       
@{server-url=http://localhost:3000}           @{renterEndpoint=; homeownerEndpoint=}

Let's travel a little farther

PS C:\Users\Cameron> $json."local-dev".server.renterEndpoint

    rejectUnauthorized host                                     port path                  
    ------------------ ----                                     ---- ----                  
                 False blah.blah.com                             443 /api/renter   

I'm sure there are other ways to extract the data you are looking for. I only just started looking at this recently. Point being is that if you know what you are looking for just use the properties of the object to get what you need. If that knowledge is not known to you Get-Member can help expose the properties to show you the data structure.

like image 91
Matt Avatar answered Sep 21 '22 12:09

Matt