Home > Enterprise >  Convert text file into Json using javascript/nodejs
Convert text file into Json using javascript/nodejs

Time:04-14

I have a text file with a lot of text in the following format:

Status: RELEASED
description: 1.3 - Azure Container Registry - Disable admin user
severity: MEDIUM
targetEffect: Preventative
release: KYVOS
releasePi: pi8
destination: N/A
alertEnabled_A: TBD
alertEnabled_B: TBD
runbookURL: TBD
Comments: ''
alertPriority: P3
CCO: '1.3'
Service: Azure Container Registry
- configRuleName: SC-AZURE-ACR-002
Status: RELEASED
description: 7.3 - Azure Container Registry - Default action deny
severity: MEDIUM
targetEffect: Preventative
release: KYVOS
releasePi: pi8
destination: N/ASC-AZURE-APPGATEWAY-005
alertEnabled_A: TBD
alertEnabled_B: TBD
runbookURL: TBD
Comments: ''
alertPriority: P3
CCO: '7.3'
Service: Azure Container Registry

I want to convert it to JSON and then write it to the appropriate file. I tried the following code:

    //Read Each line separately and add them to an array. and remove first line of your file;
    const array = file.split('\n')//.slice(1);
    // console.log('array is ', array)
    let objects = [], i, data, data2 = [];
    for(let row of array){
        i = 0;
        //separate each line data and add them to data array
        data = row.split(/  /);
        data2.push(data[2])
        objects.push({
            name: data2[i  ],
            status: data2[  i],
            severity: data2[  i],
            runbookUrl: data2[  i],
            destination: data2[  i],
            comments:data2[  i]
        })
    }

And a few iterations of it. It returns data which is duplicated and bad. I have difficulties here, can someone please suggest a solution?

Edit the json should look like

{
  configRuleName: SC-AZURE-ACR-001
  Status: RELEASED
  description: 1.3 - Azure Container Registry - Disable admin user
  severity: MEDIUM
  targetEffect: Preventative
  release: KYVOS
  releasePi: pi8
  destination: N/A
  alertEnabled_A: TBD
  alertEnabled_B: TBD
  runbookURL: TBD
  Comments: ''
  alertPriority: P3
  CCO: '1.3'
  Service: Azure Container Registry
},
{
  configRuleName: SC-AZURE-ACR-002
  Status: RELEASED
  description: 7.3 - Azure Container Registry - Default action deny
  severity: MEDIUM
  targetEffect: Preventative
  release: KYVOS
  releasePi: pi8
  destination: N/ASC-AZURE-APPGATEWAY-005
  alertEnabled_A: TBD
  alertEnabled_B: TBD
  runbookURL: TBD
  Comments: ''
  alertPriority: P3
  CCO: '7.3'
  Service: Azure Container Registry
}

CodePudding user response:

What about something along the lines of:

file=`- configRuleName: SC-AZURE-ACR-001
Status: RELEASED
description: 1.3 - Azure Container Registry - Disable admin user
severity: MEDIUM
targetEffect: Preventative
release: KYVOS
releasePi: pi8
destination: N/A
alertEnabled_A: TBD
alertEnabled_B: TBD
runbookURL: TBD
Comments: ''
alertPriority: P3
CCO: '1.3'
Service: Azure Container Registry
- configRuleName: SC-AZURE-ACR-002
Status: RELEASED
description: 7.3 - Azure Container Registry - Default action deny
severity: MEDIUM
targetEffect: Preventative
release: KYVOS
releasePi: pi8
destination: N/ASC-AZURE-APPGATEWAY-005
alertEnabled_A: TBD
alertEnabled_B: TBD
runbookURL: TBD
Comments: ''
alertPriority: P3
CCO: '7.3'
Service: Azure Container Registry`

// code starts here ///////////////////////

const lines = file.split('\n');
const json = []
for(let line of lines) {
  let [key, value] =  line.split(":")
  if (key=="- configRuleName") json.push({})
  Object.assign(json.at(-1), {[key]: value})
}

console.log(json)

  • Related