Categories
JavaScript Answers

How to create JSON string in JavaScript?

Sometimes, we want to create JSON string in JavaScript.

In this article, we’ll look at how to create JSON string in JavaScript.

How to create JSON string in JavaScript?

To create JSON string in JavaScript, we can create an object and then call JSON.stringify with it.

For instance, we write

const obj = {};
obj.name = "bob";
obj.age = 32;
obj.married = false;

const string = JSON.stringify(obj);

to create the obj object.

Then we add a few properties into it with

obj.name = "bob";
obj.age = 32;
obj.married = false;

Then we call JSON.stringify with obj to convert obj to a JSON string.

Conclusion

To create JSON string in JavaScript, we can create an object and then call JSON.stringify with it.

Categories
JavaScript Answers

How to fix Node.js can’t create Blobs with JavaScript?

Sometimes, we want to fix Node.js can’t create Blobs with JavaScript.

In this article, we’ll look at how to fix Node.js can’t create Blobs with JavaScript.

How to fix Node.js can’t create Blobs with JavaScript?

To fix Node.js can’t create Blobs with JavaScript, we can import it from the node:buffer module since Node 16.

To use it, we write

import { Blob } from "node:buffer";

const b = new Blob([]);

to import the Blob constructor with

import { Blob } from "node:buffer";

Then we use it to create the b Blob object.

We can use the cross-blob module if our app runs with older versions of Node.

To install it, we run npm i cross-blob.

Then we use it by writing

import Blob from "cross-blob";

const b = new Blob([]);

Conclusion

To fix Node.js can’t create Blobs with JavaScript, we can import it from the node:buffer module since Node 16.

Categories
JavaScript Answers

How to parse JSON array with JavaScript?

Sometimes, we want to parse JSON array with JavaScript.

In this article, we’ll look at how to parse JSON array with JavaScript.

How to parse JSON array with JavaScript?

To parse JSON array with JavaScript, we call the JSON.parse method.

For instance, we write

const myMessage = `
{
  "success": true,
  "counters": [
    {
      "counterName": "dsd",
      "counterType": "sds",
      "counterUnit": "sds"
    },
    {
      "counterName": "gdg",
      "counterType": "dfd",
      "counterUnit": "ds"
    }
  ]
}
`;

const jsonData = JSON.parse(myMessage);
for (const counter of jsonData.counters) {
  console.log(counter.counterName);
}

to call JSON.parse with the myMessage string.

Then we loop through the parsed jsonData object’s counters array property with a for-of loop.

In it, we get the counterName property of each object with counter.counterName.

Conclusion

To parse JSON array with JavaScript, we call the JSON.parse method.

Categories
JavaScript Answers

How to upload file without form with JavaScript?

Sometimes, we want to upload file without form with JavaScript.

In this article, we’ll look at how to upload file without form with JavaScript.

How to upload file without form with JavaScript?

To upload file without form with JavaScript, we can create a FormData object with the file.

For instance, we write

<input type="file" name="fileInput" />

to add a file input.

Then we write

const input = docucment.querySelector("input");
input.onchange = async (e) => {
  const formData = new FormData();
  formData.append(e.target.name, e.target.files[0]);

  const response = await fetch("/api/upload", {
    method: "POST",
    body: formData,
  });
  const result = await response.json();
  console.log(result.message);
};

to select the input with querySelector.

Then we set the input.onchange property to a function that runs when we change the file selection.

In it, we create a FormData object.

And we call append to put the file inside with key e.target.name.

e.target.name is the input’s name attribute’s value.

e.target.files[0] is the first selected file.

Then we call fetch with the URL to upload the file to and an object with the body property set to formData to make the upload request.

Conclusion

To upload file without form with JavaScript, we can create a FormData object with the file.

Categories
JavaScript Answers

How to get current URL from an iframe with JavaScript?

Sometimes, we want to get current URL from an iframe with JavaScript.

In this article, we’ll look at how to get current URL from an iframe with JavaScript.

How to get current URL from an iframe with JavaScript?

To get current URL from an iframe with JavaScript, we can use the contentWindow.location.href property.

For instance, we write

const url = document.getElementById("iframe_id").contentWindow.location.href;

to select the iframe with

document.getElementById("iframe_id")

Then we get the current URL of the iframe with the contentWindow.location.href property.

Conclusion

To get current URL from an iframe with JavaScript, we can use the contentWindow.location.href property.