0

Want to convert json output to array or list using Powershell.

I have tried converting the output using ConvertFrom-Json, then fetching the key Name and Values. Need suggestion.

json Output:

{
    "demo1": {
        "type": [
            "tuple",
            [
                [
                    "list",
                    "string"
                ],
                [
                    "list",
                    "string"
                ]
            ]
        ],
        "value": [
            [
                "123"
            ],
            [
                "456"
            ]
        ]
    },
    "demo2": {
        "type": [
            "tuple",
            [
                "string",
                "string"
            ]
        ],
        "value": [
            "abc",
            "xyz"
        ]
    }
}

Using Powershell want to convert it as below:

demo1 = [["123"],["456"]]
demo2 = ["abc","xyz"]

Thanks in advance.

js2010
  • 23,033
  • 6
  • 64
  • 66
user47
  • 105
  • 2
  • 12
  • OK so you want to just get the property "value" and make that into a array? – ArcSet May 28 '20 at 19:01
  • yes, want to get output in array @ArcSet – user47 May 28 '20 at 19:07
  • Please mark as correct answer if this is what you were looking for. – ArcSet May 28 '20 at 19:16
  • is it possible to display the output as I mentioned in expected output. I am getting this output, but not in the expected way – user47 May 28 '20 at 19:24
  • you want the out as a string? or a array? you said in the post as a array... I am confused on exactly what you are looking for... – ArcSet May 28 '20 at 19:56
  • sorry, if I confused. I want output similar to : ```demo1 = [["123"],["456"]]``` and ```demo2 = ["abc","xyz"] ```. – user47 May 28 '20 at 20:01
  • so, when I will print demo1 value, it should print as [["123"],["456"]] and for demo2 it will be ["abc","xyz"] – user47 May 28 '20 at 20:06
  • I think what i am saying is what you are asking for doesn't makes since. You asked for Arrays like `$Demo1 = "123", "456"`. Are you asking for a sting like `$demo1 = "[["123"],["456"]]"` or are you asking for an array of 2 string `$array = "demo1 = [["123"],["456"]]", "demo2 = ["abc","xyz"]"` – ArcSet May 28 '20 at 20:09
  • yes,asking output like this ```$demo1 = "[["123"],["456"]]"```, a string – user47 May 28 '20 at 20:11

2 Answers2

2

Seems like a pretty straightforward use of convertfrom-json:

$a = get-content file.json | convertfrom-json

$a.demo1.value
123
456

$a.demo2.value
abc
xyz

You want it as json?

$a.demo1.value | ConvertTo-Json -Compress
[["123"],["456"]]

$a.demo2.value | ConvertTo-Json -Compress
["abc","xyz"]
js2010
  • 23,033
  • 6
  • 64
  • 66
  • but, I want a output like : ```demo1 = [["123"],["456"]] ``` and ```demo2 = ["abc","xyz"] ```. As I want to store the values also. – user47 May 28 '20 at 20:09
  • @user47, are you thinking about Python or why do you use these brackets? `$a.demo1.value` is a list and can be stored. – Alex_P May 28 '20 at 21:07
  • @Alex_P, solved my problem, my mistake. not converting back to JSON. Thanks. – user47 May 29 '20 at 04:59
1

It should be something like:

$t = '{
    "demo1": {
        "type": [
            "tuple",
            [
                [
                    "list",
                    "string"
                ],
                [
                    "list",
                    "string"
                ]
            ]
        ],
        "value": [
            [
                "123"
            ],
            [
                "456"
            ]
        ]
    },
    "demo2": {
        "type": [
            "tuple",
            [
                "string",
                "string"
            ]
        ],
        "value": [
            "abc",
            "xyz"
        ]
    }
}'

$t = ConvertFrom-Json $t 


$t | Get-Member | Where-Object {$_.MemberType -eq 'NoteProperty'} | foreach {
$element = $_.Name

echo 'Property Name: '
echo $element
echo 'Elements: '
echo ($t.$element.value | ConvertTo-Json -Depth 99)

}

At this point, powershell core seems to behave slightly different than v3-5, so you while powershell core will give you directly the correct result older versions will need some extra handling at this point, see here: Why does powershell give different result in one-liner than two-liner when converting JSON?

Farbkreis
  • 604
  • 3
  • 12