2

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.

9
  • OK so you want to just get the property "value" and make that into a array? Commented May 28, 2020 at 19:01
  • yes, want to get output in array @ArcSet Commented May 28, 2020 at 19:07
  • Please mark as correct answer if this is what you were looking for. Commented May 28, 2020 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 Commented May 28, 2020 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... Commented May 28, 2020 at 19:56

2 Answers 2

3

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"]
Sign up to request clarification or add additional context in comments.

3 Comments

but, I want a output like : demo1 = [["123"],["456"]] and demo2 = ["abc","xyz"] . As I want to store the values also.
@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, solved my problem, my mistake. not converting back to JSON. Thanks.
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.