How to config get the first value ini file when duplicate keys

I read an ini file with duplicate keys, ie:

[SESSION]
key1=value1
key1=value2222
xxx=yyy

I used the code below to get the contents of the ini file

var fs = require('fs')
  , ini = require('ini')

var config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'))

In case the key is duplicate, the last value is taken.
The result is:

[SESSION]
key1=value2222
xxx=yyy

I would like to get the first value , is it configurable?
Thanks in advance!

[SESSION]
key1=value1
xxx=yyy

Have you read the documentation of the ini module you are using?

1 Like

Why not use the file-in node to read the file. You could read each line as a separate msg then do what you want with them.

1 Like

´╝×I have read it but have not seen the configuration write to do what I want
Link: https://github.com/npm/ini/blob/master/README.md

´╝×Because using the ini module, it will be easier to manipulate the contents of the ini file, such as getting session, key, value, ...

A quick look at the docs for that module suggest it expects the following syntax for keys that can have multiple values:

[paths.default]
datadir = /var/lib/data
array[] = first value
array[] = second value
array[] = third value

Are you able to change the ini file syntax to match that?

If not, then you won't be able to use the ini module and you'll have to figure out how to parse it for yourself using the File In node and some Function code.

1 Like

´╝×Alright, I understand. Thank you very much.

Just out of curiosity, are you creating the ini file in the first place?
if so, why are you creating the duplicates?

  • why couldn't you create a javascript object instead?
{
    "session": {
        "key1": "value1",
        "xxx": "yyy"
    }
}

then you could address it with session.key1.

Just a thought.

1 Like

Because I want to use with ini file.
I will take note of your instructions.
Thank you for your enthusiastic guidance!