简体   繁体   中英

Parsing JSON with Powershell's ConvertFrom-Json

I am trying to parse this JSON using Powershell's ConvertFrom-Json feature, but it seems to truncate the data:

{
  "MessagesMonitoring": {
    "version": 1,
    "description": "Message Description"
  },
  "data": {
    "swindon": {
      "totalMessages": 0,
      "identifier": [
        {
          "name": "ET",
          "staleCount": 4
        },
        {
          "name": "ET_2",
          "staleCount": 4
        }
      ]
    },
    "Reading": {
      "totalMessages": 0,
      "identifier": [
        {
          "name": "J3",
          "staleCount": 2
        }
      ]
    },
    "Yanki": {
      "totalMessages": 0,
      "identifier": [
        {
          "name": "UT",
          "staleCount": 4
        },
        {
          "name": "UT_2",
          "staleCount": 4
        }
      ]
    }
  }
}

Request:

 $request = 'http://localhost:8000/hi.json'
 Invoke-WebRequest $request |
 ConvertFrom-Json |
 Select swindon

Response:

StatusCode        : 200                                                         StatusDescription : OK
Content           : {
                      "MessagesMonitoring": {
                        "version": 1,
                        "description": "Message Description"
                      },
                      "data": {
                        "swindon": {
                          "totalMessages": 0,
                          "identifier": [
                            {
                              "na...

Not sure what I may be doing incorrectly. Any advise/guidance on how to parse the JSON into this format would be great.

swindon|identifier|ET|4
swindon|totalMessages|0
swindon|identifier|ET2|4

Reading|identifier|J3|2
Reading|totalMessages|0

Yanki|identifier|UT|4
Yanki|identifier|U_T|4
Yanki|totalMessages|0

You are missing a step. The Content element of the response contains the JSON, so that's what you need to feed into ConvertFrom-Json :

$request = 'http://localhost:8000/hi.json'
$resp = $(Invoke-WebRequest $request).Content | ConvertFrom-Json

Then, within the JSON you have a dictionary, within which the "data" key contains the information I think you're interested in, access it using this syntax:

$resp.data

That should get you started

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM