1

I am trying to install libraries on my databricks cluster, based on the external json file.

The file is constructed like this:

{"libraries":[

    {
      "pypi": {
        "package": "sparkmeasure==0.14.0"
      }
    },
    {
      "maven": {
        "coordinates": "azureml-core==1.20.0"
      }
    }
]
}

Then I am trying to read the file, build the request body and invoke the rest method.

$json = Get-Content $filePath| Out-String | ConvertFrom-Json -Depth 99 

$Region = $Region.Replace(" ", "")

$uri = "https://$Region.azuredatabricks.net/api/2.0/libraries/install"

$Body = @{ "cluster_id" = $ClusterId }

$Body['libraries'] = $json.libraries
$BodyText = $Body | ConvertTo-Json
Write-Output $BodyText 

Invoke-RestMethod -Uri $uri -Body $BodyText -Method 'POST' -Headers @{ Authorization = $InternalBearerToken }

This is the output body I am getting:

{
  "libraries": [
    {
      "pypi": "@{package=sparkmeasure==0.14.0}"
    },
    {
      "maven": "@{coordinates=azureml-core==1.20.0}"
    }
  ],
  "cluster_id": "myclusterid"
}

However, I only need to grab the value from the file, so my expected result looks like that:

{
  "libraries": [
    
        {
          "pypi": {
            "package": "sparkmeasure==0.14.0"
          }
        },
        {
          "maven": {
            "coordinates": "azureml-core==1.20.0"
          }
        }
    ],
  "cluster_id": "myclusterid"
}

I am very new to PowerShell, so I was wondering if there is some simple way to preserve the structure of the json file instead of having those nested structures as arrays?

Grevioos
  • 355
  • 5
  • 30
  • 1
    Add `-Depth` to the `ConvertTo-Json` as well. – Jeroen Mostert Mar 18 '21 at 16:42
  • The short of it: Unfortunately, `ConvertTo-Json` has default recursion depth of `2`, causing more deeply nested objects to be _truncated_ (cut off and represented by the result of `.ToString()`; in v7.1+ you'll at least get a _warning_ when that happens). Use the `-Depth` parameter with a sufficiently high number, as shown in the [linked post](https://stackoverflow.com/q/53583677/45375). – mklement0 Mar 18 '21 at 16:46

1 Answers1

1

You can just use simple replace in that case from the output that you are getting like below:

I am just using your Output and then taking it forward

$a = @'
{
  "libraries": [
    {
      "pypi": "@{package=sparkmeasure==0.14.0}"
    },
    {
      "maven": "@{coordinates=azureml-core==1.20.0}"
    }
  ],
  "cluster_id": "myclusterid"
}
'@
($a.Replace('@{','')).replace('}"','"')   #This is what will replace the things as per your need.

Also while converting to JSON , just add -Depth as denoted by Jeroen

Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45