I have a text file with a lot of text in the following format:
-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
I want to convert it to JSON and then write it to the appropriate file. I tried the following code:
const file = fs.readFileSync('./valuesToExtract2.txt', 'utf-8');
//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
}
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.