Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

303
Vistas
add keys to a json file, remove duplicates and write to json file in javascript

I want to add data from another Json file to another without overwriting the existing one. I just can't get any further, the console always gives me the following:

Console output 
Data 
string  
[
    "follow1",
    "follow2",
    "follow3",
    "follow4",
    "[\"follow5\",\"follow6\",\"follow7\",\"follow8\",\"follow9\"]"
]

This is my code, I would like to add the data but without square brackets and without backslashes. It would be really nice if someone could help me. Thanks very much

const user = require('./user.json');
const oldUser = user.user_follwos["user1"];
const data = require('./data.json');
const toAdd = JSON.stringify(data);

const result = JSON.stringify(toAdd);
oldUser.push(...toAdd.split(' '))
const newData = JSON.stringify(oldUser, null, 4)
console.log('\nData \n' + typeof newData + '  \n' + newData);

and here are my json files

//user.json
{
  "application_id": "123546789",
  "user_follwos": {
    "user1": [
      "follow1",
      "follow2",
      "follow3",
      "follow4"
    ],
    "user2": [
      "followA",
      "followB",
      "followC",
      "followD"
    ]
  },
 ...
 ...
}
//data.json
[
  "follow5",
  "follow6",
  "follow7",
  "follow8",
  "follow9"
]
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You should convert your data structure to JSON exactly once at the very end, immediately before you write the result to the file.

As it stands, you are converting everything to JSON at every opportunity. So every time you try to add, say, an array to your data structure you instead add a string of JSON representing that array.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You don't need to stringify your data variable because it is a javascript object so you can just concat it to the end of your existing user array.

const user = require('./user.json');
let oldUser = user.user_follwos["user1"];
const data = require('./data.json');

oldUser = oldUser.concat(data);
const newData = JSON.stringify(oldUser, null, 4);
console.log('\nData \n' + typeof newData + '  \n' + newData);

The concat method creates a new array object so you could assign the result to a new variable without overwriting your existing "oldUser" variable.

Data
string
[
    "follow1",
    "follow2",
    "follow3",
    "follow4",
    "follow5",
    "follow6",
    "follow7",
    "follow8",
    "follow9"
]
about 4 years ago · Juan Pablo Isaza Denunciar

0

First of all to do something you need both data in json

  • Make 2 arrays
  • Remove duplicates
  • Then push data without duplicates.

Put everything together

let allTogether = data.push(...oldUser);

Create unique array

uniq = [...new Set(allTogether )];

Finally set this unique data to specific key

user_follwos.user1 = uniq

Hope this is what you need

Update with example

    let user = {
    "application_id": "123546789",
    "user_follwos": {
    "user1": [
      "follow1",
      "follow2",
      "follow3",
      "follow4"
    ],
    "user2": [
      "followA",
      "followB",
      "followC",
      "followD"
    ]
  }
};

let data = [
  "follow5",
  "follow6",
  "follow7",
  "follow8",
  "follow9"
];

let oldUser = user["user_follwos"]["user1"];
console.log(`This is old user array`);
console.log(oldUser);
let allTogether = [];
allTogether.push(...data)
allTogether.push(...oldUser);
console.log(`After we put all together`);
console.log(allTogether);
uniq = [...new Set(allTogether )];
console.log(`Getting unique values`);
console.log(uniq);
oldUser = uniq;
console.log(`Now olds user is`);
console.log(oldUser);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda