从Json解析值数据数据文件 [英] Parsing Values From Json a data data file

查看:63
本文介绍了从Json解析值数据数据文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将JSON文件加载到powershell中,然后处理JSon文件中的各个节点.该文件是从现有的Json文件加载的:

I need to load a JSON file into powershell, and then process the various nodes from the JSon file. The file is loaded from an existing Json file:

$JSONContent = (Get-Content -Path "C:\JsonFile.json" -Raw) | ConvertFrom-Json

加载后,我可以通过以下方式查看数据值:

Once this is loaded, I can see the data values by:

$JSONContent | SELECT -expand Results

然后为我提供了字段和值的列表格式.

which then gives me a list format of the fields and values.

我的问题是我想遍历 $ JSONContent 对象,并为每个逻辑记录(用逗号分隔),将每个字段值连接起来以逗号分隔的字符串.

My problem is that I want to iterate through the $JSONContent object and for each logical record (delimited by comma), concatenate each field value to make a comma-separated string.

我确信答案是微不足道的,但我已尝试解决此问题.如果有任何仁慈的人可以帮助您,那么我将不胜感激.

I am sure the answer is trivial, but I have tried to resolve this. If any kind person could help with this, then I would be grateful.

作为输入的JSON文件为:

The JSON file as input is:

{
  "results": [
    {
      "id": 1,
      "id2": null,
      "first_name": "Jeanette",
      "last_name": "Penddreth",
      "email": "jpenddreth0@census.gov",
      "gender": "Female",
      "ip_address": "26.58.193.2",
      "serverlist": [
        {
          "myval1": "testdata1",
          "myval2": "testdata2",
          "myval3": "testdata3"
        }
      ],
      "aitlist": [
        {
          "ait1": "aitdata1",
          "ait2": "aitdata2"
        }
      ]
    },
    {
      "id": 2,
      "first_name": "Giavani",
      "last_name": "Frediani",
      "email": "gfrediani1@senate.gov",
      "gender": "Male",
      "ip_address": "229.179.4.212",
      "serverlist": [
        {
          "myval1": "testdata5",
          "myval2": "testdata6",
          "myval3": "testdata7"
        },
        {
          "myval1": "testdata9",
          "myval2": "testdata10",
          "myval3": "testdata11"
        }
      ],
      "aitlist": [
        {
          "ait1": "aitdata3",
          "ait2": "aitdata4"
        }
      ]
    },
    {
      "id": 3,
      "first_name": "Noell",
      "last_name": "Bea",
      "email": "nbea2@imageshack.us",
      "gender": "Female",
      "ip_address": "180.66.162.255"
    },
    {
      "id": 4,
      "first_name": "Willard",
      "last_name": "Valek",
      "email": "wvalek3@vk.com",
      "gender": "Male",
      "ip_address": "67.76.188.26"
    }
  ]
}

目标是使用第一个记录的示例以每个逗号分隔的字符串结尾每个节点的值:

The objective is to end up with the values from each node as a comma-separated string like this, using the example of the first record:

1,"Jeanette","Penddreth","jpenddreth0@census.gov","Female", "26.58.193.2","testdata1","testdata2","testdata3","aitdata1","aitdata2"

推荐答案

以下命令通过

The following command recursively walks all arrays and objects in the object graph parsed from your JSON input via ConvertFrom-Json and outputs each object's leaf property values (those containing primitive types) as a comma-separated list:

(Get-Content -Raw C:\JsonFile.json | ConvertFrom-Json).Results | ForEach-Object {
  # Helper script block that walks an object graph and outputs all leaf property values.
  $sb = {
    foreach ($el in @($args[0])) {
      if ($el -is [System.Management.Automation.PSCustomObject]) { # a complex object -> recurse
        foreach ($prop in $el.psobject.Properties) { & $sb $prop.Value } # recurse on the properties
      }
      else {
        $el # a leaf value -> output it
      }
    }
  }

  # Call the script block with the input object at hand and collect all values.
  $values = & $sb $_

  # Enclose string values in embedded "...", join with "," and output.
  $values.ForEach({ if ($_ -is [string]) { "`"$_`"" } else { $_} }) -join ','

}

注意:必须使用 $ args [0] 周围的 @(...),以确保 null JSON值不会丢失.

Note: The @(...) around $args[0] is required to ensure that null JSON values aren't lost.

使用您的样本输入将产生:

With your sample input this yields:

1,"Jeanette","Penddreth","jpenddreth0@census.gov","Female","26.58.193.2","testdata1","testdata2","testdata3","aitdata1","aitdata2"
2,"Giavani","Frediani","gfrediani1@senate.gov","Male","229.179.4.212","testdata5","testdata6","testdata7","testdata9","testdata10","testdata11","aitdata3","aitdata4"
3,"Noell","Bea","nbea2@imageshack.us","Female","180.66.162.255"
4,"Willard","Valek","wvalek3@vk.com","Male","67.76.188.26"

这篇关于从Json解析值数据数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆