Forem

Cover image for JSON: Que bruxaria é essa?!?!
ananopaisdojavascript
ananopaisdojavascript

Posted on

3

JSON: Que bruxaria é essa?!?!

Começando pela definição...

JSON (JavaScript Object Notation - Notação de Objeto JavaScript) é um modo de representar dados estruturados na linguagem JS. É composto por uma chave (propriedade) e um valor.

{
    "nomeDoPokemon": "Pikachu",
    "tipoDePokemon": "Elétrico"
}
Enter fullscreen mode Exit fullscreen mode

Tipos básicos

Os tipos básicos presentes no JSON são:

  • Strings (Cadeia de texto ou caracteres)
  • Number (Números inteiros ou reais)
  • Booleano (Verdadeiro ou falso)
  • Nulo (null)
{
  "nomeDoPokemon": "Pikachu",
  "tipoDePokemon": "Elétrico",
  "numeroDoPokemon": 5,
  "isThisPokemonNormal": false,
  "nullPokemon": null
}
Enter fullscreen mode Exit fullscreen mode

Como eu converto um texto em JSON?

É só usar a função JSON.parse() e colocar como parâmetro o texto que será convertido.

const texto = '{"atributo1": "valor 1", "atributo2": 23}';

const objeto = JSON.parse(texto);

console.log(objeto);
Enter fullscreen mode Exit fullscreen mode

Como eu converto um objeto JSON em texto?

É só usar a função JSON.stringify() e colocar como parâmetro o objeto que será convertido.

const objeto = {
    "atributo1": "valor 1",
    "atributo2": 23
};

const texto = JSON.stringify(objeto);

console.log(texto);
Enter fullscreen mode Exit fullscreen mode

Como ler os dados dos atributos de um objeto JSON?

Podemos ler os dados dos atributos de um objeto JSON por ponto ou por colchetes

Notação de ponto

const texto = {
    "atributo1": "valor 1",
    "atributo2": 23
};

console.log(texto.atributo1);
Enter fullscreen mode Exit fullscreen mode

Notação de colchetes

const texto = {
    "atributo1": "valor 1",
    "atributo2": 23
};

console.log(texto["atributo1"]);
Enter fullscreen mode Exit fullscreen mode

Adicionar uma nova propriedade

Podemos também incluir uma nova propriedade ao objeto JSON

const texto = {
    "atributo1": "valor 1",
    "atributo2": 23
};

texto.atributo3 = null;

console.log(texto);
Enter fullscreen mode Exit fullscreen mode

Excluir uma propriedade

Podemos também excluir uma propriedade com o operador delete.

delete texto.atributo3;
Enter fullscreen mode Exit fullscreen mode

E aí? Gostaram? Até a próxima anotação! 😊

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay