0

I am trying to read a text file and convert it to a javascript object using Node JS. My file looks like as follow:

Package: libseccomp2
Depends: libc6 (>= 2.14)
Description: high level interface to Linux seccomp filter
 This library provides a high level interface to constructing, analyzing
 and installing seccomp filters via a BPF passed to the Linux Kernel's
 prctl() syscall.


Package: libperl5.26
Depends: libbz2-1.0, libc6 (>= 2.23), libdb5.3, libgdbm-compat4, libgdbm5 (>= 1.12), zlib1g (>= 1:1.2.2.3), perl-modules-5.26 (>= 5.26.1-6ubuntu0.3)
Description: shared Perl library
 This package contains the shared Perl library, used by applications
 which embed a Perl interpreter.
 .
 It also contains the architecture-dependent parts of the standard
 library (and depends on perl-modules-5.26 which contains the
 architecture-independent parts).

I wanted to output it like so:

[{
Package: "libseccomp2",
Depends: ["libc6"]
Description: "high level interface to Linux seccomp filter
 This library provides a high level interface to constructing, analyzing
 and installing seccomp filters via a BPF passed to the Linux Kernel's
 prctl() syscall."
},
{
Package: "libperl5.26",
Depends: ["libbz2-1.0", "libc6"]
Description: "high level interface to Linux seccomp filter
 This library provides a high level interface to constructing, analyzing
 and installing seccomp filters via a BPF passed to the Linux Kernel's
 prctl() syscall."
}]

This is what I have so far but it keeps on giving me errors. Thank you in advance for your help

fs.readFile('file.txt', "utf8", (err, data) => {
  if (err) {
    console.error(err)
    return
  }
  let obj = {};
  let line = data.toString().split("\n");
  for (let i = 0; i<line.length; i++) {
      let newLine = line[i].split(":");

      obj[newLine[0]] = newLine[1].split(" ");
  }
  console.log(obj);

})
1
  • What errors are you getting ? Did you try debugging those errors ? From what I can see, newLine[1].split(" ") could have a problem for the lines that do not contain a ":" because newLine[1] will be undefined Commented Nov 3, 2019 at 17:20

1 Answer 1

1

Your source file looks like it is derived from a YAML file. If it was formatted properly, you could use an npm library like "yaml" to parse the file directly to a JS object.

A properly formatted YAML file looks like:

-
 Package: libseccomp2
 Depends: libc6 (>= 2.14)
 Description: high level interface to Linux seccomp filter
  This library provides a high level interface to constructing, analyzing and installing seccomp filters via a BPF passed to the Linux Kernel's prctl() syscall.

-
 Package: libperl5.26
 Depends: libbz2-1.0, libc6 (>= 2.23), libdb5.3, libgdbm-compat4, libgdbm5 (>= 1.12), zlib1g (>= 1:1.2.2.3), perl-modules-5.26 (>= 5.26.1-6ubuntu0.3)
 Description: shared Perl library
  This package contains the shared Perl library, used by applications
  which embed a Perl interpreter.
  .
  It also contains the architecture-dependent parts of the standard
  library (and depends on perl-modules-5.26 which contains the
  architecture-independent parts).

I would check to see if your source text file can be generated into proper YAML first, or if you are stuck with your current text file, try to edit it to proper YAML by using a YAML validator.

Sign up to request clarification or add additional context in comments.

2 Comments

it does look like a YAML but since the file contains a lot of / the validator says its not a valid one
It isn't valid as it currently is formatted. I posted a corrected version above. Try running my version through a YAML to JSON convertor, you'll get the format you're looking for. The only difference between your version and mine is the indentation, and the item separator "-".

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.