Skip to content

ConvertFrom-Json should have option to produce hashtables instead of objects #3159

Closed

Description

@vors

Json is very convenient to glue tools together.
For example, both python and powershell have ability to serialize and de-serialize anything into json without a schema.
Unfortunately, after de-serialization from json in PowerShell, you get immutable objects.
If you just get mutable hashtables, life would be much better: i.e. you can create some object in python, toss it to powershell, enhance with some info and inspect the complete result.

One of the date points about a similar request: http://stackoverflow.com/questions/3740128/pscustomobject-to-hashtable

It's a more generic question, but I think that in context of json it's especially desirable.

Activity

  1. added
    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif
    Issue-Enhancementthe issue is more of a feature request than a bug
    on Feb 16, 2017
  2. Jaykul commented on Feb 16, 2017

    @Jaykul
    Contributor

    In theory, this makes a lot of sense to me.

    Although honestly, my biggest problem with json and PowerShell is that it can't ever produce the same json it received, I'm pretty sure this would make that worse unless ConvertTo-Json is fixed first ;-)

  3. vors commented on Feb 16, 2017

    @vors
    CollaboratorAuthor

    Joel Bennett (@Jaykul) can you provide a specific example?

  4. joeyaiello commented on Feb 16, 2017

    @joeyaiello
    Contributor

    sergei (@vors): #2448 and #3153 are both examples.

    Could you clarify your original point with an example, though? This seems to work fine to me:

    PS C:\temp> cat .\foo.txt
    {
            "a":1,
            "b":2
    }
    PS C:\temp> $a = cat .\foo.txt | ConvertFrom-Json
    PS C:\temp> $a
    
    a b
    - -
    1 2
    
    
    PS C:\temp> $a.b = 3
    PS C:\temp> $a
    
    a b
    - -
    1 3
    
    PS C:\temp> $a | ConvertTo-Json
    {
        "a":  1,
        "b":  3
    }
  5. Jaykul commented on Feb 18, 2017

    @Jaykul
    Contributor

    Joey Aiello (@joeyaiello): Exactly. It seems like I run into one or the other of those every time.
    As far as mutability, you missed it ;-)

    $user = '{ "name": "Joel", "city":"Rochester" }' | ConvertFrom-Json
    $user.name = "Joel Bennett" # sure, that's fine
    $user.account = "Jaykul" # nope. Can't add things.
  6. vors commented on Feb 20, 2018

    @vors
    CollaboratorAuthor

    Sorry for long reply time, dropped out of my attention.

    Could you clarify your original point with an example, though?

    Sure, in your example $a is PsCustomObject, and not a hashtable.
    The reason why it's important is because it's very common to enumerate thru the key-value pairs and apply some transformation, or try to find some value. With PSObject instead of hashtable it ever requires some step like https://stackoverflow.com/a/34383413/1355726 or doing some Get-Memeber with NoteProperty, which is pretty obscure way to do it.

  7. markekraus commented on Feb 20, 2018

    @markekraus
    Contributor

    Hmm isn't this issue resolved? ConvertFrom-Json -AsHashtable was added in #5043.

  8. vors commented on Feb 21, 2018

    @vors
    CollaboratorAuthor

    Ah indeed, awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifIssue-Enhancementthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions