pass contents of a json file field to a variable in powershell

Viewed 196

I have a json file and I want to pass the value of a field to a variable the json file has the following structure

{
"actions": {
   "send_dian": false,
   "send_email": false
 },
  "invoice": {
    "number": "990001344",
    "invoice_type_code": "FACTURA_VENTA",
    "numbering": {
       "resolution_number": "18760000001",
       "prefix": "MSTL",
       "flexible": true
       },
    "customer": {
       "email": "TOROFABIAN@GMAIL.COM",
       "phone": "3136490333",
       "company_name": "LUIS FABIAN TORO GUTIERREZ"
       }
   }
  }

and I want to pass the value of the "number" field to a variable like this:

$archivo = "archivo1.json"
$body = Get-Content $archivo 
$numerofolio1 = $body.psobject.properties["number"].value
Write-Host "numero de folio:" $numerofolio1
$numerofolio1

$numerofolio2 = $body.psobject.properties["invoice.number"].value
Write-Host "numero de folio:" $numerofolio2
$numerofolio2

but neither of the two ways sends me the value of "number".

how should I do it? Thanks

1 Answers

You need to convert the json to objects with ConvertFrom-Json:

$body = Get-Content $archivo |ConvertFrom-Json

$body.invoice.number # this will now give you the string "990001344"
Related