search and replace string javascript

Asked

Viewed 202 times

1

I have the following syntax:

        var json_pessoa = JSON.stringify(json_pessoa["pessoa"]); // o valor do json_pessoa é "{"razao_social":"asd","nome_fantasia":"asd","rg_insc_estadual":"asd"}" , ele já é iniciado com as aspas
        var json_teste1 = '"{"usuario":"';
        var json = frase.replace(json_pessoa.substring(1, 1), json_teste1);

the goal is to replace the first character which is a double quote (") with the json_teste1 variable, but it is an error in debugging and does not even report the type of error.

the string I expect to return is:

{"usuario":{"razao_social":"asd","nome_fantasia":"asd","rg_insc_estadual":"asd"}}
  • You can put in question the expected return?

  • @Jeffersonquesado , I asked the question

1 answer

3


For example you show no need to join strings this way. Simply have an object and make a json of it. That is to say:

var pessoa = '{"razao_social":"asd","nome_fantasia":"asd","rg_insc_estadual":"asd"}'
var objeto = {
  usuario: JSON.parse(pessoa)
};
var resultado = JSON.stringify(objeto);
console.log(resultado);

  • that’s what I needed, for lack of knowledge I was trying to do a gambiarra using replace, thanks again!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.