-2

I have a powershell json output in this format.

{
    "Name":  "web-app",
    "BuildingBlock":  "create-web-app",
    "TemplateName":  "qa-bb-create-web-app-V1.1.1",
    "TemplateID":  1809
},
{
    "Name":  "web-app",
    "BuildingBlock":  "destroy-web-app",
    "TemplateName":  "qa-bb-destroy-web-app-V1.1.1",
    "TemplateID":  1810
 }

What I need to do is get he first key value wherever it is duplicate take it as a common parameter and make the third keyvalue as the nested key and provide the fourth keyvalue as the value. SOmething like this. Can anyone provide the solution in powershell.

{
   "web-app":{
               "create-web-app":"1809",
               "destroy-web-app":"1810"
}
arco444
  • 22,002
  • 12
  • 63
  • 67
B S
  • 1
  • 1
  • If you use `Convertfrom-json` and use the dot reference for BuildingBlock, then you can get them – Ranadip Dutta Apr 15 '19 at 08:51
  • 2
    SO is not a place where other people write code for you. What have you tried so far (show your code), and what *particular* problem (in your code) do you need help with? – Ansgar Wiechers Apr 15 '19 at 09:02

1 Answers1

0

May be your JSON are not correctly formated. You can try something like this?

$varJSON=@"
{
    "Name":  "web-app",
    "BuildingBlock":  "create-web-app",
    "TemplateName":  "qa-bb-create-web-app-V1.1.1",
    "TemplateID":  1809
},
{
    "Name":  "web-app",
    "BuildingBlock":  "destroy-web-app",
    "TemplateName":  "qa-bb-destroy-web-app-V1.1.1",
    "TemplateID":  1810
 }
"@

$varJSON='{"Root": [' + $varJSON + "]}"

$JSON=$varJSON | ConvertFrom-Json
$JSON.Root | group name | %{

[pscustomobject]@{
$_.Name=$_.Group.BuildingBlock


}

} | ConvertTo-Json
Esperento57
  • 16,521
  • 3
  • 39
  • 45