locked
Loading a Powershell object using a JSON string RRS feed

  • Question

  • I'm having trouble with using classes. Here is my code:

    class MyClass {
        [string]$name
        [string]$description
    }

    $myObject = [MyClass]::new()

    $jsonText="[{`"name`": `"TestName`",`"description`": `"TheDescription`", `"color`":`"red`"}]"

    $myObject = ($jsonText | convertfrom-json)

    The problem is that $myObject is of type MyClass before the line "$myObject = ($jsonText | convertfrom-json)."  After that line, it becomes a System.Object.  I need $myObject to stay of type MyClass so that if the JSON string contains values which do not reflect members of the class, an exception would be tossed.

    Wednesday, December 5, 2018 6:14 PM

All replies

  • "The ConvertFrom-Json cmdlet converts a JavaScript Object Notation (JSON) formatted string to a custom PSCustomObject object that has a property for each field in the JSON string. JSON is commonly used by web sites to provide a textual representation of objects."

    ConvertFrom-Json will create a PS object - it is not aware of type MyClass.  You will need filter the desired results with other tooling.

    Wednesday, December 5, 2018 6:35 PM
  • So basically there is no serialization/deserialization and no way to cast.  Awesome.  Go team Microsoft.
    Wednesday, December 5, 2018 6:47 PM
  • It is just converting Json text to objects.  It is not Java.  In PS objects are all either custom or typed dotNet objects.  PS knows nothing about Java.

    Json in Java and on webs is used to manage and pst objects.  Json objects are not typed in any way.  This is a major deficiency of Java and Json.  XML is a more complete and portable notation but web developers couldn't figure it out so they invented Json.  

    There are projects underway to add strong typing to Json.  This is idiotic since it will end up being just another XML.  All of this was played with 20+ years ago and the industry chose XML over simpler forms.

    Json does not create classes.  It is an 'object" specifier.  Java classes are not Json.

    Json stands for "JavaScript Object Notation" (not Java) .  Json is an industry/web standard for transmitting objects as text.  It is not aa system or language object with methods although it is possible to use Java to create active objects.  This is not available in PowerShell but the objects can have script methods added.


    \_(ツ)_/

    Wednesday, December 5, 2018 8:10 PM
  • If you want error checking (and other stuff) you'll have to code that in the class. Something like this:

    class MyClass 
    {
        hidden [string]$_name
        hidden [string]$_description
    
        MyClass() {}
    
        MyClass([System.Object[]]$JSON) {
            if (!($JSON -is "System.Object[]")
                {
                    Throw "JSON string must be converted using ConvertFrom-JSON"
                    return
                }
            $JSON | Get-Member -MemberType NoteProperty | foreach {
                Switch ($_.Name)
                {
                    'name'         {$this._name = $JSON.name}
                    'description'  {$this._description = $JSON.description}
                    #'color'        $this._color = $JSON.color}                              <==== not implemented. Color is undefined in the set of class instance values
                    default        { Throw "Unknown name in JSON string" ; return }
                }
            }
        }
    
        # GET / SET for 'Name'
        [string]Name() { return $this._name }
        [void]Name([string]$NewName) {$this._name = $NewName}
    
        # GET / SET for 'Description'
        [string] Description() { return $this._description }
        [void]   Description([string]$NewDescription) {$this._description = $NewDescription}
    }


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Thursday, December 6, 2018 4:44 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Tuesday, December 11, 2018 6:37 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Monday, December 24, 2018 5:52 AM