How to make CRUD operations in JSON
Tags: javascript, json, howto, crud
Published: 2018-09-23 01:14:43

---
title: How to make CRUD operations in JSON
published: true
description: Steep by steep to better understand JSON
tags: JavaScript, JSON, HowTo, CRUD
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/fg9d53mh00zp3r2f5ez8.png
series: json-helpful-tips
---

---

Before starting with CRUD operations, recommend learn about:

{% link https://dev.to/equiman/how-to-create-properties-from-variables-in-json-55of %}

---

CRUD operations are things that you going to make all your life as a programmer. 

With JSON can use a clever data structure to avoid brute force search with for loops. Using the "character" name as "Primary Key".


# Game of Thrones Example

## Data

```javascript
  var got = {
    "Jon Snow": {"name": "Kit Harington", "gender": "Male", "house": "Starks"},
    "Daenerys": {"name": "Emilia Clarke", "gender": "Female", "house": "Targaryens"},
    "Tyrion": {"name": "Peter Dinklage", "gender": "Male", "house": "Lanisters"}
  }
```

## Add character (**C**reate):

{% runkit
// hidden setup JavaScript code goes in this preamble area
  var got = {
    "Jon Snow": {"name": "Kit Harington", "gender": "Male", "house": "Starks"},
    "Daenerys": {"name": "Emilia Clarke", "gender": "Female", "house": "Targaryens"},
    "Tyrion": {"name": "Peter Dinklage", "gender": "Male", "house": "Lanisters"}
  }
%}
// Click [RUN] button to see result
var characterObject = {
  "name": "Bella Ramsey", 
  "gender": "Female", 
  "house": "Mormonts"
};
var character = "Lyanna";
got[character] = characterObject;
console.log(got)
{% endrunkit %}

## Find character (**R**ead):

{% runkit
// hidden setup JavaScript code goes in this preamble area
  var got = {
    "Jon Snow": {"name": "Kit Harington", "gender": "Male", "house": "Starks"},
    "Daenerys": {"name": "Emilia Clarke", "gender": "Female", "house": "Targaryens"},
    "Tyrion": {"name": "Peter Dinklage", "gender": "Male", "house": "Lanisters"}
  }
%}
// Click [RUN] button to see result
var character = "Tyrion"; // Or whatever
var result = got[character];
console.log(result)
console.log(result.name)
console.log(result.gender)
console.log(result.house)
{% endrunkit %}

## Update character (**U**pdate):

{% runkit
// hidden setup JavaScript code goes in this preamble area
  var got = {
    "Jon Snow": {"name": "Kit Harington", "gender": "Male", "house": "Starks"},
    "Daenerys": {"name": "Emilia Clarke", "gender": "Female", "house": "Targaryens"},
    "Tyrion": {"name": "Peter Dinklage", "gender": "Male", "house": "Lanisters"}
  }
%}
// Click [RUN] button to see result
var character = "Jon Snow";
got[character].house = "Targaryens"
var result = got[character];
console.log(result)
{% endrunkit %}

## Remove Character (**D**elete):

{% runkit
// hidden setup JavaScript code goes in this preamble area
var got = {
    "Jon Snow": {"name": "Kit Harington", "gender": "Male", "house": "Starks"},
    "Daenerys": {"name": "Emilia Clarke", "gender": "Female", "house": "Targaryens"},
    "Tyrion": {"name": "Peter Dinklage", "gender": "Male", "house": "Lanisters"}
}
%}
// Click [RUN] button to see result
var character = "Tyrion";
delete got[character];
console.log(got)
{% endrunkit %}

# Alternative Structure

Maybe it's a little good looking, but you need to deal with array of positions on CRUD operations.

{% runkit
// hidden setup JavaScript code goes in this preamble area
%}
//Data
  var got = {
    "index": {
        "Jon Snow": 0, "Daenerys": 1, "Tyrion": 2
    },
    "data": [
        {"name": "Kit Harington", "gender": "Male", "house": "Starks"},
        {"name": "Emilia Clarke", "gender": "Female", "house": "Targaryens"},
        {"name": "Peter Dinklage", "gender": "Male", "house": "Lanisters"}
    ]
  }

  var character = "Daenerys";
  var index = got.index[character];
  var data = got.data[index];
  console.log(data);
{% endrunkit %}

---

**That’s All Folks!**
**Happy Coding** 🖖

[![beer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkgsx4aw6qn9i9ax6as9.png)](https://github.com/sponsors/deinsoftware)