The first example:
ConvertFrom-Json $USERX | ConvertTo-Json | Set-Clipboard
Getting properties by name: ConvertFrom-Json $ORDER | select order*The first example:
ConvertFrom-Json $USERX | ConvertTo-Json | Set-Clipboard
Getting properties by name: ConvertFrom-Json $ORDER | select order*Edit: Yes, it does.
'{ "a": { "b": { "c": { "d": { "e": 5 } } } } }' | ConvertFrom-Json | ConvertTo-Json
{
"a": {
"b": {
"c": "@{d=}"
}
}
}
So you have to remember to set `-Depth 100` for every invocation of `ConvertTo-Json`. You also can't set it higher because it has a hard-coded limit of 100, so hopefully you never deal with JSON that has more nesting than that.Yeesh. I like PS, but this one commandlet's design has always baffled me.
> echo '{ "a": { "b": { "c": { "d": { "e": 5 } } } } }' | from json | to toml
[a.b.c.d]
e = 5 gci | select -first 1 | ConvertTo-Json -Depth 2
gci | select -first 1 | ConvertTo-Json -Depth 3
gci | select -first 1 | ConvertTo-Json -Depth 4
one at a time.Or leave it as it is and expect the user to map the complex values into hashtables. Eg in your example, make the user pipe the objects through `%{ @{ Name = $_.Name; Mode = $_.Mode; } }` first.
In any case, I don't know about other PS users, but all my command-lines that ended with ConvertTo-Json either started with ConvertFrom-Json (transforming an existing JSON file), or started with HashTables (building a JSON file from scratch) or a mix of the two. Therefore I always wanted everything to be serialized, and the `-Depth` parameter was always a nuisance.
EDIT: I see your point now, thank you for explaining
It can be something as trivial as "PSHashTable is allowed to serialize without limit. Every other type is limited to N levels, unless the user sets `$SERIALIZATION_LIMIT[System.Type]` to some other value." Or make the `-Depth` parameter a `Dictionary<System.Type, int>`.
By that logic they should also add -MaxArrayLength to make the parser stop parsing long JSON arrays, -MaxProperties to make it only parse object properties up to a limit, -MaxStringLength to only make it parser strings up to a certain length...
It's completely pointless.
From-JSON wouldn’t have this concern, but to-JSON would
>Uncaught TypeError: cyclic object value
If browser JS engines can detect circular references, the PS serializer can too.
It sounds like whoever implemented this just didn't know what they were doing.
I've completely rid myself of GNU core utils with just pwsh.
I completely agree with the parent that PowerShell blows the existing shell options out of the water. Having pipelines with objects rather than just text makes everything so much easier. Instead of spending an hour futzing with awk or regex or applicatioons like jq to parse values from command results you can just access what you want directly and get on with your work.
1. https://docs.microsoft.com/en-us/powershell/scripting/instal...
For instance on my system:
Get-ChildItem -> gci or ls
Select-String -> grepNice list of aliases though.
Three commands that are useful to memorize, though, (particularly if you're having trouble remembering names) are Get-Command, Get-Alias (also with -Definition), and Get-Member. Get-Command gets you info about a command, like if it's an alias or not, and the path if it's a unix command. Get-Alias shows you all the active aliases, and Get-Alias -Definition shows you the active aliases for a given command. Get-Member shows you all the members of an object which can help you with Select-Object and Where-Object and so on.
grep -rl $pattern | $othercommand
So far I've got this (deliberately avoiding aliases for this example) in the above grep's command's stead: Get-ChildItem -Recurse -Path * | Select-String $pattern
But I just cannot fathom how to break from the table format to pass the lines to $othercommand.How does one pass a pwsh list to a single command? Using %{}/For-Each {} wouldn't work because it would invoke $othercommand for each line.
| format-list *
to investigate it, and you'll see a bunch of members. In this case if what you want is just the matched string, you want to expand the `Matches` property of the object, then expand the value property of each of those. In this case the term 'expand' is key: if you just use `select` it'll return you the property, name and all. Use `-expand` to return the plain array. So something like: Get-ChildItem -Recurse -Path * | Select-String $pattern | select -expand matches | select -expand value
which you can then use how you like. An alternate form is (Get-ChildItem -Recurse -Path * | Select-String $pattern).Matches.Value
because the . notation for member access works on all items in an array (Matches in this case). Get-ChildItem -Recurse -File | Select-String "$pattern" -List -SimpleMatch -CaseSensitive | Select-Object -ExpandProperty path
You will want to only search in files for this to not ouput some lines of InputStream if there is a match in the actual path.
If $pattern is an actual regex you will want to drop SimpleMatch.
In case you want some proper powershell objects you might want to pipe this into Get-Item.If you do write such a post make sure to let me know so I can link it from mine! You could write a response "here's how I'd do all the stuff in that post in Powershell with no extra tools" that would be very cool :)