0

Using powershell how remove a property synonymMaps in a json.

{
    "name":  "dev-contentitem-text-index",
    "defaultScoringProfile":  null,
    "fields":  [
                   {
                       "name":  "id",
                       "type":  "Edm.String",
                       "searchable":  false,
                       "filterable":  false,
                       "retrievable":  true,
                       "sortable":  false,
                       "facetable":  false,
                       "key":  true,
                       "indexAnalyzer":  null,
                       "searchAnalyzer":  null,
                       "analyzer":  null,
                       "synonymMaps":  ""
                   },
                   {
                       "name":  "myvalue",
                       "type":  "Edm.String",
                       "searchable":  false,
                       "filterable":  false,
                       "retrievable":  true,
                       "sortable":  true,
                       "facetable":  false,
                       "key":  true,
                       "indexAnalyzer":  null,
                       "searchAnalyzer":  null,
                       "analyzer":  null,
                       "synonymMaps":  ""
                   }
               ]
}

I tried which remove synonymMaps but I loose the top level property name and defaultScoringProfile. I need the whole thing intact but with synonymMaps removed.

$indexDefinition  = Get-Content $storedIndex.FullName

$indexDefinition = ($indexDefinition| ConvertFrom-Json)

$indexDefinition.fields|Select-Object * -ExcludeProperty "*synonymMaps*"
3
  • Does this answer your question? Remove JSON object properties that match regular expression in PowerShell Commented Jan 17, 2020 at 9:52
  • this is what I tried Get-Content $storedIndex.FullName -Raw|ConvertFrom-Json|Select-Object * -ExcludeProperty * synonymMaps * | ConvertTo-Json. It did not work. I think i have go the depth wrong could you help me refine it. not sure how to Commented Jan 17, 2020 at 9:59
  • the second option worked better but then it does not remove the extra comma in the last line before synonmMaps Commented Jan 17, 2020 at 10:27

1 Answer 1

0

This is a nested json-structure, you need recursive function for it:

$json = @"
{
    "name":  "dev-contentitem-text-index",
    "defaultScoringProfile":  null,
    "fields":  [
                   {
                       "name":  "id",
                       "type":  "Edm.String",
                       "searchable":  false,
                       "filterable":  false,
                       "retrievable":  true,
                       "sortable":  false,
                       "facetable":  false,
                       "key":  true,
                       "indexAnalyzer":  null,
                       "searchAnalyzer":  null,
                       "analyzer":  null,
                       "synonymMaps":  ""
                   },
                   {
                       "name":  "myvalue",
                       "type":  "Edm.String",
                       "searchable":  false,
                       "filterable":  false,
                       "retrievable":  true,
                       "sortable":  true,
                       "facetable":  false,
                       "key":  true,
                       "indexAnalyzer":  null,
                       "searchAnalyzer":  null,
                       "analyzer":  null,
                       "synonymMaps":  ""
                   }
               ]
}

"@

function Remove-Property {
  param(
    [Parameter(Mandatory, ValueFromPipeline, Position = 0)]
    [object] $inputObject,
    [Parameter(Mandatory, Position = 1)]
    [string] $namePattern
  )
  process {
    foreach ($element in $inputObject) {
      foreach ($propName in $element.psobject.Properties.Name) {
        if ($propName -like $namePattern) {
          $element.psobject.Properties.Remove($propName)
        }
        else {
            if( $element.$propName ) {
                Remove-Property -InputObject $element.$propName -namePattern $NamePattern | Out-Null
            }
        }
      }
    }
    return $inputObject
  }
}


# convert JSON to PSObject
$obj     = ConvertFrom-Json $json

# remove entries
$newObj  = $obj | Remove-Property -NamePattern 'synonymMaps*'

# convert back to json
$newJson = $newObj | ConvertTo-Json
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.