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.

Categories
JavaScript Answers

How to check if JavaScript object is JSON?

Sometimes, we want to check if JavaScript object is JSON.

In this article, we’ll look at how to check if JavaScript object is JSON.

How to check if JavaScript object is JSON?

To check if JavaScript object is JSON, we can use the JSON.parse method.

For instance, we write

const isJson = (data) => {
  try {
    const testIfJson = JSON.parse(data);
    if (typeof testIfJson === "object") {
      return true;
    } else {
      return false;
    }
  } catch {
    return false;
  }
};

to call JSON.parse with data to parse the data string.

If its type is 'object', then it’s JSON is it’s not null.

Otherwise, it’s not JSON.

If JSON.parse can’t parse data, an error will be thrown and false is returned since it’s not JSON.

Conclusion

To check if JavaScript object is JSON, we can use the JSON.parse method.

Categories
JavaScript Answers

How to debug FormData.append(“key”, “value”) is not working with JavaScript?

Sometimes, we want to debug FormData.append("key", "value") is not working with JavaScript.

In this article, we’ll look at how to debug FormData.append("key", "value") is not working with JavaScript.

How to debug FormData.append("key", "value") is not working with JavaScript?

To debug FormData.append("key", "value") is not working with JavaScript, we can loop through the values with a loop.

For instance, we write

for (const data of formData) {
  console.log(data);
}

to loop through the entries of the formData FormData object with a for-of loop.

In it, we log the data value.

This works because a FormData object is an iterable object.

Conclusion

To debug FormData.append("key", "value") is not working with JavaScript, we can loop through the values with a loop.