Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse YAML file in Powershell

I want to parse an YAML file from IBM API Connect in a PowerShell file. I won't be able to put third party packages or DLLs since security review won't agree with it.

---
product: "1.0.0"
info:
  name: "api2product"
  title: "API2product"
  version: "1.0.0"
visibility:
  view:
    enabled: true
    type: "public"
    tags: []
    orgs: []
  subscribe:
    enabled: true
    type: "authenticated"
    tags: []
    orgs: []
apis:
  api1:
    $ref: "api1_1.0.0.yaml"
  api2:
    $ref: "api2_1.0.0.yaml"
  api3:
    $ref: "api3_1.0.0.yaml"
  api4:
    $ref: "api4_1.0.0.yaml"
  api5:
    $ref: "api5_1.0.0.yaml"
plans:
  default:
    title: "Default Plan"
    description: "Default Plan"
    approval: false
    rate-limit:
      hard-limit: false
      value: "100/hour"

I am interested to get only the API YAML files associated with it for which I have googled and developed a sample PowerShell code which is actually running.

$text = Get-Content -Path "C:\Users\abhi\Desktop\projects\TRYG\GitLab\APIPOC\test.yaml"
$regex = '(?ms)apis:(.+?)plans:'
$text = $text -join "`n"
$OutputText = [regex]::Matches($text, $regex) |
              foreach {$_.Groups[1].Value -split $regex}
$OutputText = $OutputText.Replace("`$ref:", "")
$OutputText = $OutputText.Replace(":", "=")
$OutputText = $OutputText.Replace("=`n", "=")
$OutputText = $OutputText.Replace("`"", "")
$AppProps = ConvertFrom-StringData ($OutputText)
$AppProps.GetEnumerator() | ForEach-Object {$_.Value}
[string[]]$l_array = $AppProps.Values | Out-String -Stream

Is there any simple way to achieve this instead of multiple replacements in the string?

like image 471
abhi88 Avatar asked Mar 19 '19 14:03

abhi88


People also ask

Can PowerShell read YAML?

Adding YAML Support to PowerShell PowerShell has built-in support for handling JSON content, but not for YAML. Fortunately, there is a module that extends PowerShell to support YAML files. This module is called PowerShell-yaml, and the latest version as of this writing is 0.4.

What is parse YAML?

PyYAML is a YAML parser and emitter for Python. Using the PyYAML module, we can perform various actions such as reading and writing complex configuration YAML files, serializing and persisting YMAL data. Use it to convert the YAML file into a Python dictionary.

What is the difference between YAML and JSON?

The design goal of JSON is to be as simple as possible and be universally usable. This has reduced the readability of the data, to some extent. In contrast, the design goal of YAML is to provide a good human-readable format and provide support for serializing arbitrary native data structures.

What is a YAML file?

YAML is a data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain't markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.


1 Answers

I suggest you fork an open licensed module like the Phil-Factor/PSYaml as this isn't a trivial task.

Then you can keep an isolated version that you maintain your self.

like image 72
Dennis Avatar answered Oct 18 '22 16:10

Dennis