0

Below is the auto generated text file raw content.

Application Name: XKHGKSGHXSNBXS
Secret Key:       NDRkNDJjOTAtZjBhYi00NmMyLWJlMzctYjkwYTNlMjNmZTQy=
Application Name: HVABSVGJCXJGsx
Secret Key:       OTlmMmMxYjgtZjEzMS00MTkwLWI1NDQtYjI2MTc1MTk1ZTkz=
Application Name: khsxsvxhjvsGJJHJ
Secret Key:       NDRkNDJjOTAtZjBhYi00NmMyLWJlMzctYjkwYTNlMjNmZTQy=
Application Name: HDFACVXJAGSV
Secret Key:       OTlmMmMxYjgtZjEzMS00MTkwLWI1NDQtYjI2MTc1MTk1ZTkz=
Application Name: WCVDACsBHBHvsx
Secret Key:       NDRkNDJjOTAtZjBhYi00NmMyLWJlMzctYjkwYTNlMjNmZTQy=
Application Name: 90GVASXSACVSA
Secret Key:       OTlmMmMxYjgtZjEzMS00MTkwLWI1NDQtYjI2MTc1MTk1ZTkz=
Application Name: NBSANX0067Jtudent
Secret Key:       NDRkNDJjOTAtZjBhYi00NmMyLWJlMzctYjkwYTNlMjNmZTQy=
Application Name: ABNSVXNBSXdfert
Secret Key:       OTlmMmMxYjgtZjEzMS00MTkwLWI1NDQtYjI2MTc1MTk1ZTkz=

Now I want to convert this text file into json file using powerShell.

1> If we convert this directly using -ConvertToJson then it gives unwanted o/p.

2> If we convert text file into op1.csv and then again convert to op2.json it also gives unwanted o/p(output).

3> If we remove extra \tabs and Whitespaces from the file and then repeating the step 2 gives trash.

I want that o/p should be as follow.

[
  {
    "ApplicationName" : "POADFJBKJBHACBNASCVX",
    "SecretKey"       : "NDRkNDJjOTADFHACVJNmZTQy"
  },
  {
    "ApplicationName" : "NBFSVJBHACBNASCVX",
    "SecretKey"       : "NDRkNDJjOTADFHACVJNmZTQy"
  },
  {
    "ApplicationName" : "VDFACBHACBNASCVX",
    "SecretKey"       : "NDRkNDJjOTADFHACVJNmZTQy"
  },
  {
    "ApplicationName" : "ADFCASBKJBHACBNASCVX",
    "SecretKey"       : "NDRkNDJjOTADFHACVJNmZTQy"
  },
  {
    "ApplicationName" : "DFCKJBHACBNASCVX",
    "SecretKey"       : "NDRkNDJjOTADFHACVJNmZTQy"
  },
  {
    "ApplicationName" : "FERDBKJBHACBNASCVX",
    "SecretKey"       : "NDRkNDJjOTADFHACVJNmZTQy"
  },
  ...
]

Note: there may be N number of blocks in Text File.

3 Answers 3

2

You could use ConvertFrom-StringData to create objects from reading two lines at a time. Those objects could then be converted to JSON.

Get-Content file.txt -ReadCount 2 | Foreach-Object {
    $hash = [ordered]@{}
    $_ -replace ':','=' | ConvertFrom-StringData | Foreach-Object { 
        $_.GetEnumerator() | Foreach-Object {
            $hash[$_.key] = $_.value
        }
    }
    [pscustomobject]$hash
} | ConvertTo-Json
Sign up to request clarification or add additional context in comments.

Comments

1

You could use switch -Regex -File to loop over the lines in the file and create an object as you go. Finally, convert this array of objects to JSON:

# loop through the file line-by-line
$result = switch -Regex -File 'D:\Test\AutoGeneratedFile.txt' {
    '^Application Name:.*' {
        $item = [PsCustomObject]@{ ApplicationName = ($_ -split ':', 2)[1].Trim(); SecretKey = '' }
    }
    '^Secret Key:.*' {
        $item.SecretKey = ($_ -split ':', 2)[1].Trim()
        # output the completed item
        $item
    }
}

# convert the array of objects to Json
$result | ConvertTo-Json

Output:

[
    {
        "ApplicationName":  "XKHGKSGHXSNBXS",
        "SecretKey":  "NDRkNDJjOTAtZjBhYi00NmMyLWJlMzctYjkwYTNlMjNmZTQy="
    },
    {
        "ApplicationName":  "HVABSVGJCXJGsx",
        "SecretKey":  "OTlmMmMxYjgtZjEzMS00MTkwLWI1NDQtYjI2MTc1MTk1ZTkz="
    },
    {
        "ApplicationName":  "khsxsvxhjvsGJJHJ",
        "SecretKey":  "NDRkNDJjOTAtZjBhYi00NmMyLWJlMzctYjkwYTNlMjNmZTQy="
    },
    {
        "ApplicationName":  "HDFACVXJAGSV",
        "SecretKey":  "OTlmMmMxYjgtZjEzMS00MTkwLWI1NDQtYjI2MTc1MTk1ZTkz="
    },
    {
        "ApplicationName":  "WCVDACsBHBHvsx",
        "SecretKey":  "NDRkNDJjOTAtZjBhYi00NmMyLWJlMzctYjkwYTNlMjNmZTQy="
    },
    {
        "ApplicationName":  "90GVASXSACVSA",
        "SecretKey":  "OTlmMmMxYjgtZjEzMS00MTkwLWI1NDQtYjI2MTc1MTk1ZTkz="
    },
    {
        "ApplicationName":  "NBSANX0067Jtudent",
        "SecretKey":  "NDRkNDJjOTAtZjBhYi00NmMyLWJlMzctYjkwYTNlMjNmZTQy="
    },
    {
        "ApplicationName":  "ABNSVXNBSXdfert",
        "SecretKey":  "OTlmMmMxYjgtZjEzMS00MTkwLWI1NDQtYjI2MTc1MTk1ZTkz="
    }
]

Comments

0

You should use ConvertTo-Json Microsoft.PowerShell.Utility

ConvertTo-Json
              [-InputObject] <Object>
              [-Depth <Int32>]
              [-Compress][-EnumsAsStrings]
              [-AsArray]
              [-EscapeHandling <StringEscapeHandling>][<CommonParameters>]

Read more about it on Microsoft Docs

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.