fetch javascript get json code example

Example 1: fetch json post

(async () => {
  const rawResponse = await fetch('https://httpbin.org/post', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({a: 1, b: 'Textual content'})
  });
  const content = await rawResponse.json();

  console.log(content);
})();

Example 2: javascript fetch json

fetch('./yourjson.json')
  .then((response) => response.json())
  .then((data) => {
  	console.log(data);
  })

Example 3: javascript fetch api get

fetch('http://example.com/movies.json')
  .then(response => response.json())
  .then(data => console.log(data));

Tags:

Misc Example