0

I need to process a series of data, where each line has the user ID and the data I want to process. I thought a good solution would be to read these files and create a JSON where the keys are the ids and the values are all the data associated with that id.

[
  id1 : 
        {
          name: "..."
          age: "..."
          date_of_birth: "..."
        },
  id2 : 
        {
          name: "..."
          age: "..."
          date_of_birth: "..."
        },
  ...
]

However, I'm not sure how I can dynamically add more information to the objects associated with the ID. At each reading of the line of my file, I must look in my dictionary for the ID. If there is already a record of this ID in the dictionary, I should just add a new line in the object associated with it. Otherwise, I must create this key/value pair and add a new line to the object associated with it. Preferably, I would like to build this dictionary in descending order of ids. Any idea how I can build this structure?

Liam
  • 27,717
  • 28
  • 128
  • 190
  • Sounds like you should be using a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) – Liam Feb 02 '22 at 13:24
  • A side comment about the JSON structure in your question - the top-level markup should be `{ }` not `[ ]` since it has keys. Also, JSON keys can only be strings and unlike JavaScript syntax, the key names must be inside double quote marks. – Noam Feb 02 '22 at 13:32

1 Answers1

0

if one id can have several instances , it is better to use this structure

{
  "id1" : [
        {
          name: "name1",
          age: 1,
          date_of_birth: "date1"
        }
       ],
    
  "id2" :[ 
        {
          name: "name2",
          age: 2,
          date_of_birth: "date2"
        }
       ]
}
Serge
  • 40,935
  • 4
  • 18
  • 45