I have to combine following 2 JSON files because the API which i use has a limit of 1000.
js1.json:
{
"total": 1311,
"limit": 1000,
"offset": 0,
"data": [
// 1000 Users
]
}
js2.json:
{
"total": 1311,
"limit": 1000,
"offset": 1000,
"data": [
// 311 Users
]
}
What i need:
{
"data": [
// All 1311 Users
]
}
I have seen a few posts where it is written you could just add the JSON files together. But in the end I always get this:
Current PS Code:
$js1 = Invoke-RestMethod 'API Request with offset 0' -Method 'GET' -Headers $headers -Body $body | ConvertTo-Json
$js2 = Invoke-RestMethod 'API Reuqest with offset 1000' -Method 'GET' -Headers $headers -Body $body | ConvertTo-Json
$js1 + $js2 | Out-File -FilePath .\cloudUsers.json
What you can do if you only want the content of the data field then you can extract it to a PSObject and merge them and convert it back to JSON if that is the format you want.
$json1 = (Get-Content .\js1.json | ConvertFrom-Json)
$json2 = (Get-Content .\js2.json | ConvertFrom-Json)
$data = $json1.data + $Json2.data
$data | ConvertTo-Json
If you just want to combine the data
values and create a new JSON with a data
property, you can do the following:
$js1 = Get-Content js1.json | ConvertFrom-Json
$js2 = Get-Content js2.json | ConvertFrom-Json
[pscustomobject]@{data = $js1.data + $js2.data} | ConvertTo-Json
If you want to maintain the original JSON schema, you can update the properties accordingly:
$js1 = Get-Content js1.json | ConvertFrom-Json
$js2 = Get-Content js2.json | ConvertFrom-Json
$js1.limit = $js1.total
$js1.data += $js2.data
$js1 | ConvertTo-Json
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.