0

I have the following situation below. I'd like to add a name to each item in the array.

  • First item needs to have a name instead of being an anonymous object. How do I do this?
  • Items need to have an name derived from the Technical Name, so 988 instead of N988AB1.
  • Structure of array is changed to an object in the result.

Current situation:

{
    "Category": [{
            "TechnicalName": "N988AB1",
            "Name": "House"
        },
        {
            "TechnicalName": "H181AG3",
            "Name": "Apartment"
        },
        {
            "TechnicalName": "X123XY5",
            "Name": "Villa"
        }
    ]
}

Desired situation:

{
    "Data": {
        "988": {
            "TechnicalName": "N988AB1",
            "Name": "House"
        },
        "181": {
            "TechnicalName": "H181AG3",
            "Name": "Apartment"
        },
        "0123": {
            "TechnicalName": "X0123XY5",
            "Name": "Villa"
        }
    }
}
1
  • You should really post your coding attempt. Commented Mar 30, 2022 at 18:56

1 Answer 1

2

One way of tackling this: (You can copy and paste the full contents of the code box to try it out. I've commented what various steps are doing)

$ExistingJson = @"
{
    "Category": [{
            "TechnicalName": "N988AB1",
            "Name": "House"
        },
        {
            "TechnicalName": "H181AG3",
            "Name": "Apartment"
        },
        {
            "TechnicalName": "X123XY5",
            "Name": "Villa"
        }
    ]
}
"@

# Convert the json string to an object we can manipulate.
$ExistingObj = $ExistingJson | ConvertFrom-Json

# Create an empty hashtable for the new items we are creating.
$HT = @{}
foreach ($Category in $ExistingObj.Category) {
    # Match any digits, grouped as "name", after any character [A-Za-z] which are followed by [A-Za-z] - this is pretty basic and may need to be changed if your TechnicalName string changes, or you want different information from it.
    [System.Void]($Category.TechnicalName -match "(?<=[A-Za-z])(?<name>\d+)(?=[A-Za-z])")
    $NewItem = [PSCustomObject]@{
        TechnicalName = $Category.TechnicalName
        Name          = $Category.Name
    }

    # Add a new entry to the hashtable with the discovered digits by it's group name "name" and the object created above with the existing information as it's value.
    $HT.Add($Matches["name"], $NewItem)
}

# Create a new object with a Data property with the value as the hashtable.
$NewObj = [PSCustomObject]@{
    Data = $HT
}

# Convert it back to Json
$NewObj | ConvertTo-Json

Results in:

{
  "Data": {
    "988": {
      "TechnicalName": "N988AB1",
      "Name": "House"
    },
    "181": {
      "TechnicalName": "H181AG3",
      "Name": "Apartment"
    },
    "123": {
      "TechnicalName": "X123XY5",
      "Name": "Villa"
    }
  }
}
Sign up to request clarification or add additional context in comments.

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.