powershell: json -> csv

Иногда бывает необходимость найти что-то в длинном JSON например вот такого вида:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{
    "relations": [{
            "ID": 8449,
            "LinkType": 2,
            "Changed Date": "2019-08-23T08:36:52.627Z",
            "Revised Date": "9999-01-01T00:00:00Z",
            "AuthorizedAddedDate": "2019-08-23T08:36:52.627Z",
            "AuthorizedRemovedDate": "9999-01-01T00:00:00Z",
            "Changed By": 23953,
            "Revised By": -1,
            "Lock": false,
            "Comment": ""
        }, {
            "ID": 8452,
            "LinkType": 2,
            "Changed Date": "2019-08-23T08:55:54.503Z",
            "Revised Date": "9999-01-01T00:00:00Z",
            "AuthorizedAddedDate": "2019-08-23T08:55:54.503Z",
            "AuthorizedRemovedDate": "9999-01-01T00:00:00Z",
            "Changed By": 23953,
            "Revised By": -1,
            "Lock": false,
            "Comment": ""
        }, {
            "ID": 8451,
            "LinkType": 2,
            "Changed Date": "2019-08-23T08:55:54.503Z",
            "Revised Date": "9999-01-01T00:00:00Z",
            "AuthorizedAddedDate": "2019-08-23T08:55:54.503Z",
            "AuthorizedRemovedDate": "9999-01-01T00:00:00Z",
            "Changed By": 23953,
            "Revised By": -1,
            "Lock": false,
            "Comment": ""
        }, {
            "ID": 8450,
            "LinkType": 2,
            "Changed Date": "2019-08-23T08:35:54.387Z",
            "Revised Date": "9999-01-01T00:00:00Z",
            "AuthorizedAddedDate": "2019-08-23T08:35:54.387Z",
            "AuthorizedRemovedDate": "9999-01-01T00:00:00Z",
            "Changed By": 19418,
            "Revised By": -1,
            "Lock": false,
            "Comment": ""
        }, {
            "ID": 8453,
            "LinkType": 2,
            "Changed Date": "2019-08-23T08:46:00.823Z",
            "Revised Date": "9999-01-01T00:00:00Z",
            "AuthorizedAddedDate": "2019-08-23T08:46:00.823Z",
            "AuthorizedRemovedDate": "9999-01-01T00:00:00Z",
            "Changed By": 19418,
            "Revised By": -1,
            "Lock": false,
            "Comment": ""
        }, {
            "ID": 8448,
            "LinkType": 2,
            "Changed Date": "2019-08-23T08:36:10.91Z",
            "Revised Date": "9999-01-01T00:00:00Z",
            "AuthorizedAddedDate": "2019-08-23T08:36:10.91Z",
            "AuthorizedRemovedDate": "9999-01-01T00:00:00Z",
            "Changed By": 23953,
            "Revised By": -1,
            "Lock": false,
            "Comment": ""
        }, {
            "ID": 8454,
            "LinkType": 2,
            "Changed Date": "2019-08-23T08:57:02.873Z",
            "Revised Date": "9999-01-01T00:00:00Z",
            "AuthorizedAddedDate": "2019-08-23T08:57:02.873Z",
            "AuthorizedRemovedDate": "9999-01-01T00:00:00Z",
            "Changed By": 19418,
            "Revised By": -1,
            "Lock": false,
            "Comment": ""
        }
    ]
}

Намного удобнее искать значения когда такие данные представлены в виде таблицы. решаем проблему такой командой:

1
Get-Content -Path '.\relations.json' | ConvertFrom-Json | Select-Object -ExpandProperty relations | ConvertTo-Csv -NoTypeInformation -Delimiter ';' | Out-File -FilePath 'relations.csv' -Encoding utf8