13

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?

1 Answer 1

13

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Matt! It just wasn't immediately obvious that the data was there.
@dprothero Agreed. Just seemed like it had to be there somewhere :)
Good answer.. FYI, having been bitten myself, I use one trick to confirm things like these.. ConvertTo-Json $json -Depth 10 and it will show the correct and entire structure (Depth 10, can use higher number), nicely formatted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.