Title: ConvertFrom-Json sends objects converted from a JSON array as an *array* through the pipeline. · Issue #3424 · PowerShell/PowerShell · GitHub
Open Graph Title: ConvertFrom-Json sends objects converted from a JSON array as an *array* through the pipeline. · Issue #3424 · PowerShell/PowerShell
X Title: ConvertFrom-Json sends objects converted from a JSON array as an *array* through the pipeline. · Issue #3424 · PowerShell/PowerShell
Description: Note: Perhaps the behavior in question will rarely be noticed as problematic in the real world, so perhaps the only thing that's needed is adding a note to the documentation - I'm unclear on whether this ultimately a non-issue. alpha.17 ...
Open Graph Description: Note: Perhaps the behavior in question will rarely be noticed as problematic in the real world, so perhaps the only thing that's needed is adding a note to the documentation - I'm unclear on whethe...
X Description: Note: Perhaps the behavior in question will rarely be noticed as problematic in the real world, so perhaps the only thing that's needed is adding a note to the documentation - I'm unclear o...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/3424
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ConvertFrom-Json sends objects converted from a JSON array as an *array* through the pipeline.","articleBody":"Note: **Perhaps the behavior in question will rarely be noticed as problematic in the real world, so perhaps the only thing that's needed is adding a note to the _documentation_** - I'm unclear on whether this ultimately a non-issue.\r\n\r\nalpha.17 fixed issue #3153 with respect to `ConvertTo-Json` no longer wrapping `ConvertFrom-Json` array output in an extraneous object with `value` and `count` properties.\r\n\r\nHowever, `ConvertFrom-Json` still sends the objects resulting from converting a JSON _array_ as a _single array_ through the pipeline, which is a deviation from the usual cmdlet behavior of _unwrapping_ collections and sending the items _one by one_.\r\n\r\n```powershell\r\n\u003e ('[ 1, 2 ]' | ConvertFrom-Json | Measure-Object).Count\r\n1 # !! PS array `1, 2` was sent as a *single* object through the pipeline.\r\n```\r\n\r\n**Workaround**: Enclose the `ConvertFrom-Json` part of the pipeline in `(...)`, which forces enumeration:\r\n\r\n```powershell\r\n\u003e (('[ 1, 2 ]' | ConvertFrom-Json) | Measure-Object).Count\r\n2 # OK, (...) forced enumeration\r\n```\r\n\r\nAlternatively, simply insert a `Write-Output` pipeline segment:\r\n\r\n```powershell\r\n\u003e ('[ 1, 2 ]' | ConvertFrom-Json | Write-Output | Measure-Object).Count\r\n2 # OK, Write-Output forced enumeration\r\n```\r\n\r\n\u003csup\u003eNote: On _Windows PowerShell_, as of v5.1, you paradoxically need `Write-Output -NoEnumerate` to prevent unwrapping from getting applied to the individual array elements as well.\u003c/sup\u003e\r\n\r\nOn the plus side, as @PetSerAl points out, this preserves the input structure in a round-trip in the case of _single_-element JSON arrays:\r\n\r\n```powershell\r\n\u003e '[ 1 ]' | ConvertFrom-Json | ConvertTo-Json\r\n[\r\n 1\r\n]\r\n```\r\nIt is only `ConvertFrom-Json`'s send-the-array-as-a-whole approach that enables the above round trip.\r\n\r\nBy contrast, **when you start with a PS array**, it doesn't work that way (which illustrates the difference in pipeline behavior):\r\n\r\n```powershell\r\n\u003e , 1 | ConvertTo-Json\r\n1 # !! Input PS array was unwrapped on sending it through the pipeline\r\n```\r\n\r\nTo preserve the array, you must _explicitly_ _nest_ the array:\r\n\r\n```powershell\r\n\u003e , , 1 | ConvertTo-Json\r\n[\r\n 1\r\n]\r\n\r\n# Alternative: explicitly prevent enumeration\r\n\u003e Write-Output -NoEnumerate (, 1) | ConvertTo-Json\r\n[\r\n 1\r\n]\r\n```\r\n\r\nEnvironment data\r\n----------------\r\n\r\n```powershell\r\nPowerShell Core v6.0.0-alpha (v6.0.0-alpha.17) on Darwin Kernel Version 16.4.0: Thu Dec 22 22:53:21 PST 2016; root:xnu-3789.41.3~3/RELEASE_X86_64\r\n```\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2017-03-25T18:28:36.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":47},"url":"https://github.com/3424/PowerShell/issues/3424"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:cef3a3ef-602a-da3b-bf74-98d16933a0ea |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B59A:21FDDF:2D0CFEF:40EFD6B:6A551A5D |
| html-safe-nonce | 6e2b4f9794cf456a4befe586d2833488add081a483bf74c5d6d2ff22cf2dfd9d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNTlBOjIxRkRERjoyRDBDRkVGOjQwRUZENkI6NkE1NTFBNUQiLCJ2aXNpdG9yX2lkIjoiMTQ3ODYzNjc0MTA3MDA5NjI5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 0c31f7384543b65a0bdfdd9684da02eaec7eb85feee0840523d8f2db61a09810 |
| hovercard-subject-tag | issue:216993837 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/3424/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ae9c046686e29bd00b32f233e077e383c617b8b623b5b7b11185af9a02cfa5c5/PowerShell/PowerShell/issues/3424 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ae9c046686e29bd00b32f233e077e383c617b8b623b5b7b11185af9a02cfa5c5/PowerShell/PowerShell/issues/3424 |
| og:image:alt | Note: Perhaps the behavior in question will rarely be noticed as problematic in the real world, so perhaps the only thing that's needed is adding a note to the documentation - I'm unclear on whethe... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mklement0 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 682d273eacb2ac51680c6eb9c0b270f029f7ce74c32090f319083c34497e28a5 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 3b6d37c6470adadff4194742daaab9a817cc4980 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width