0

I have a geojson file that needs to be submitted to an API. I am modifying a preexisting powershell script to execute this query but I am having trouble getting the geojson to parse correctly in the query string to pass to the API. Powershell is not my language at all but I've been able to get it to read the geojson. My print statement in my code looks like this:

$inputjson = Get-Content -Raw -Path C:/path/to/file.geojson | ConvertFrom-Json
Foreach ($feature in $inputjson.features){
    $gjson = $feature.geometry
    Write-Host $gjson

My output is then:

@{type=Polygon; coordinates=System.Object[]}

I have tried ToString() or even casting $gjson as string to try and force this to read as it appears in the file. In python I can do this easily enough but this is a complex script I don't have the time to rewrite from scratch. How do I get this to translate to string correctly? What exactly does that '@' decorator connote in json subfield in Powershell?

  • `@{ }` indicates a HashTable, which is what `ConvertFrom-Json` creates out of a JSON string. `ConvertFrom-Json` creates nested HashTables or Arrays to represent the objects. Flattening a HashTable isn't necessarily simple with the output of `ConvertFrom-Json` any more that deserializing and flattening JSON is. Can you provide an example JSON string for a feature, and desired output? – Bacon Bits Feb 09 '18 at 18:05
  • I ended up giving up on this effort and ending up writing the whole process in python instead of modifying the powershell script I was passed. After further research it appeared multipolygon geojson and powershell simply don't play well together. – Echelon_One Jul 05 '18 at 12:38

2 Answers2

1

The point is that GeoJSON is not a flat object. This means that you have to (recursively) iterate through each embedded object to get each containing subitem:

$inputjson = Get-Content -Raw -Path C:/path/to/file.geojson | ConvertFrom-Json
Foreach ($feature in $inputjson.features){
    $gjson = $feature.geometry
    Write-Host "Type = " $gjson.Type
    Foreach ($coordinate in $coordinates){
        Write-Host "coordinate = " $coordinate

Maybe this will help you: $inputjson | Flatten, see: https://stackoverflow.com/a/46081131/1701026

iRon
  • 20,463
  • 10
  • 53
  • 79
0

@{key=value} is an hashtable

it's not clear what you are trying to achieve, maybe you want to reconvert ypur geometry to json ? if so

 $feature.geometry | ConvertTo-Json

is what you need

Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103