# Week 08

## JSON (JavaScript Object Notation)

```
{
  "title": "Oldboy",
  "director": "Park Chan-wook",
  "year": 2003,
  "rating": 8.4
}
```

```
{
  "firstName": "John",
  "lastName": "Smith",
  "age": 25,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021"
  },
  "phoneNumber": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "fax",
      "number": "646 555-4567"
    }
  ],
  "gender":{
    "type":"male"
  }
}
```

{% embed url="<https://www.w3schools.com/js/js_json_intro.asp>" %}

### We have played with JSON already.......

```
var ball = {    
  x : 300,    
  y : 200,    
  xspeed : 4,    
  yspeed : -5
}
```

#### When we access the values in the JSON object.

```
var person = {
  "firstName": "John",
  "lastName": "Smith",
  "age": 25,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021"
  },
  "phoneNumber": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "fax",
      "number": "646 555-4567"
    }
  ],
  "gender":{
    "type":"male"
  }
}
```

```
person.fisrtName
person.lastName
person.address.streetAddress
```

#### <https://editor.p5js.org/kdoodoo/sketches/HkMKXi42Q>

### Let's make your own .json file.

#### food.json

```
{
 "name":"curry",
 "red": 100,
 "green":200,
 "blue":100,
}
```

#### Let's validate

#### <https://jsonlint.com/>

```
{
	"name": "curry",
	"red": 100,
	"green": 200,
	"blue": 100
}
```

#### <https://editor.p5js.org/kdoodoo/sketches/Hk-YisNnm>

### JSON with \[Array]

<https://github.com/dariusk/corpora/tree/master/data>

```
{
	"description": "A list of car manufacturers.",
	"cars": [
		"Abarth",
		"Alfa Romeo",
		"Aston Martin",
		"Audi",
		"Bentley",
		"BMW",
		"Bugatti",
		"Cadillac",
		"Caparo",
		"Caterham",
		"Chevrolet",
		"Chrysler",
		"Citroen",
		"Dacia",
		"Daihatsu",
		"Dodge",
		"Ferrari",
		"Fiat",
		"Fisker",
		"Ford",
		"Gordon Murray",
		"Holden",
		"Honda",
		"Hummer",
		"Hyundai",
		"Infiniti",
		"Jaguar",
		"Jeep",
		"Kia",
		"Koenigsegg",
		"KTM",
		"Lamborghini",
		"Lancia",
		"Land Rover",
		"Lexus",
		"Lotus",
		"Maserati",
		"Maybach",
		"Mazda",
		"McLaren",
		"Mercedes-Benz",
		"MG",
		"Mini",
		"Mitsubishi",
		"Morgan",
		"Nissan",
		"Noble",
		"Pagani",
		"Peugeot",
		"Porsche",
		"Proton",
		"Renault",
		"Rolls-Royce",
		"Seat",
		"Skoda",
		"Smart",
		"Subaru",
		"Suzuki",
		"Tata",
		"Tesla Motors",
		"Toyota",
		"Vauxhall",
		"VW",
		"Volvo"
	]
}
```

### We use   AAA.BBB\[ i ] &#x20;

#### i as an index

```
var maker = carmaker.cars[4]; // Bentley
```

#### <https://editor.p5js.org/kdoodoo/sketches/B1Gxvn42m>

## API (Application programming interface)

{% embed url="<https://en.wikipedia.org/wiki/Application_programming_interface>" %}

#### It can be anything like CSV, XML, TXT, PDF but we want "JSON!"&#x20;

#### Why?? J = Javascript

## JSON VIEWER

#### Before >>>

![](https://3008602149-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LENuoEyK2N20-qT-Oc8%2F-LPxUisnhUFxKJKtn2Rw%2F-LPxUp1iczYmG1MvgS-O%2F%EC%A0%9C%EB%AA%A9%20%EC%97%86%EC%9D%8C2666.png?alt=media\&token=5a436377-a0e3-4cea-803d-1edae82f1be1)

#### After >>>

![](https://3008602149-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LENuoEyK2N20-qT-Oc8%2F-LPxQkBotl0lnpEnRqkI%2F-LPxUb8kE2ad4Ac63ZEy%2F%EC%A0%9C%EB%AA%A9%20%EC%97%86%EC%9D%8C2.png?alt=media\&token=74fb426d-909f-4fc6-a559-4aab31d19795)

#### <https://samples.openweathermap.org/data/2.5/forecast?q=M%C3%BCnchen,DE&appid=b6907d289e10d714a6e88b30761fae22>

{% embed url="<https://chrome.google.com/webstore/search/json>" %}

![](https://3008602149-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LENuoEyK2N20-qT-Oc8%2F-LPxVKaSBNPKYkmRphhM%2F-LPxVOI7dQk1VZextFJH%2F%EC%A0%9C%EB%AA%A9%20%EC%97%86%EC%9D%8C26667.png?alt=media\&token=adec1916-c3ce-461d-97b1-9ae3b1b72779)

## Simple API example

{% embed url="<https://openweathermap.org/price>" %}

#### Sign Up here:

#### <https://openweathermap.org/appid>

![](https://3008602149-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LENuoEyK2N20-qT-Oc8%2F-LQ-WZCVaKfrc4b2SdDu%2F-LQ-XX_V8YfhvqKAv1E8%2FScreen%20Shot%202018-10-30%20at%2012.55.13%20AM.png?alt=media\&token=4c2f5fe9-9e24-4918-891a-8ad3b69df612)

### Try it here: Weather in London:

<https://samples.openweathermap.org/data/2.5/weather?q=London,uk>

#### ?q=london,uk

#### ?q=london,uk& \_\_\_\_\_\_\_\_ = \_\_\_\_\_\_\_\_ &\_\_\_\_\_\_\_\_=\_\_\_\_\_\_\_\_\_

<https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=e4d37ccc2c4f3977c2e466fa78b38fcb>

#### \&units=metric

#### ?lat=35\&lon=139&#x20;

```
https://api.openweathermap.org/data/2.5/forecast?q=Seoul&APPID=e4d37ccc2c4f3977c2e466fa78b38fcb&units=metric
```

```
https://api.openweathermap.org/data/2.5/forecast?q=Seoul&APPID=e4d37ccc2c4f3977c2e466fa78b38fcb
```

#### <https://editor.p5js.org/kdoodoo/sketches/HJfYl643m>

## Assignment

<https://goo.gl/forms/lZGI0Nf5iW6GT1ei1>
