Check if string is valid JSON array or object – PHP

Check if string is valid JSON array or object in PHP using PHP build-in functions. There are 2 ways to check the validity.

1st method (json_decode)

If you are getting a PHP variable who’s value can be either a JSON array or an JSON object, and you want to check if the JSON string is either an array or an object, then you can use PHP built-in json_decode function.

This function accepts the string as a parameter and return the decoded array or object. If it fails to decode the JSON string, it will return the null value.

2nd method (json_last_error)

Second method to check the validity of JSON string is to use the PHP built-in json_last_error function. This will return the error code of last decode attempt on JSON string. It returns a numerical value. 0 means there is no error. An enum JSON_ERROR_NONE can be used to compare the value. You can check all other possible values from PHP official documentation.

3rd method (json_validate, requires PHP >= 8.3)

However, there is a third method too but it requires PHP >= 8.3. It is the latest version of PHP at the time of writing this. So it is not upgraded in many of the websites, that is why you should use this only when you are sure that your site is running on PHP 8.3 or above. A new function json_validate is introduction in PHP 8.3. It accepts only 1 parameter, a JSON string, and return true or false if the string is a valid JSON or not.

Here is the complete code on how you can do that:

<?php

$json = "adnan-tech.com";

$json = '["Adnan"]';

$json = '{"name": "Adnan"}';

echo $json . " = ";

// if (json_validate($json))
// {

// }

$result = json_decode($json);

// if (json_last_error() === JSON_ERROR_NONE)

if ($result != null)
{
    echo "Valid <br />";

    if (is_array($result))
    {
        echo "Array";
    }

    if (is_object($result))
    {
        echo "Object";
    }
}
else
{
    echo "In-valid";
}

I have given 3 possible values of JSON string.

  1. It can be an in-valid string.
  2. It can be a valid array.
  3. It can be a valid object.

Also, I have written both 2 conditions that can be used to check the validity of JSON string.

That’s how you can check if the string is valid JSON array or an object in PHP. If you want to know how to encode the data from database into an array, check our tutorial.

OnChildAdded called multiple times – Firebase

If you are working in Firebase, then you might have encountered an issue with child_added event or onChildAdded method. It is called multiple times on page load. However, it should have been called only when a new child is added. As per Firebase documentation, this is the expected behavior. It first returns all the existing childs and then listen for new added children. But there might be a scenario where you want to perform some action only on new child, not on existing children.

So I have a Firebase database and I have a users array in it.

firebase-database-users
Firebase database users

Step 1

The way I got around this problem, is by first I am looping through all the records in Firebase.

<script type="module">
  // Import the functions you need from the SDKs you need
  import { initializeApp } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-app.js";
  import { getDatabase, ref, get, onChildAdded } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-database.js";
  // TODO: Add SDKs for Firebase products that you want to use
  // https://firebase.google.com/docs/web/setup#available-libraries

  // Your web app's Firebase configuration
  const firebaseConfig = {
    apiKey: "",
    authDomain: "",
    projectId: "",
    storageBucket: "",
    messagingSenderId: "",
    appId: ""
  };

  // Initialize Firebase
  const app = initializeApp(firebaseConfig);
  const database = getDatabase(app);

  const usersRef = ref(database, "users")

  // [step 2]

  get(usersRef).then(function (snapshot) {

    if (snapshot.exists()) {

      snapshot.forEach(function (childSnapshot) {

        // [step 3]

      })

    }

  })

</script>

Note: Make sure to use your own Firebase configurations. If you do not know how to setup Firebase configurations, you can follow our guide.

Step 2

After that, we will create a variable that will count the length of users array. Write the following line in place of // [step 2].

let count = 0

This will initialize the variable “count”.

Step 3

Then increment that variable’s value by 1. Following code goes in place of // [step 3].

count++

if (snapshot.size == count) {

  // [step 4]

}

This will also checks when all the element of array has been looped.

Step 4

Initialize another variable that will tell if all the elements has been fetched.

let hasInitializedFirebase = false

Then we will set its value to true at // [step 4].

setTimeout(function () {
  hasInitializedFirebase = true;
}, 1000)

We are adding a delay of 1 second to prevent the onChildAdded event to be called immidiately.

Step 5

Last step is to call the onChildAdded event and check if the variable hasInitializedFirebase is true.

onChildAdded(usersRef, async function (data) {

  if (hasInitializedFirebase) {

    console.log(data)
    
  }

})

If you reload the page now, you will not see the onChildAdded logs, but as soon as you insert a new element in array, you will see that the console.log(data) has been called.

Complete code

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Test</title>
</head>
<body>

    <script type="module">
      // Import the functions you need from the SDKs you need
      import { initializeApp } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-app.js";
      import { getDatabase, ref, get, set, push, onChildAdded, onChildChanged } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-database.js";
      // TODO: Add SDKs for Firebase products that you want to use
      // https://firebase.google.com/docs/web/setup#available-libraries

      // Your web app's Firebase configuration
      const firebaseConfig = {
        apiKey: "",
        authDomain: "",
        projectId: "",
        storageBucket: "",
        messagingSenderId: "",
        appId: ""
      };

      // Initialize Firebase
      const app = initializeApp(firebaseConfig);
      const database = getDatabase(app);

      const usersRef = ref(database, "users")
      let count = 0
      let hasInitializedFirebase = false

      get(usersRef).then(function (snapshot) {

        if (snapshot.exists()) {

          snapshot.forEach(function (childSnapshot) {

            count++

            if (snapshot.size == count) {

              setTimeout(function () {
                hasInitializedFirebase = true;
              }, 1000)

            }

          })

        } else {

          setTimeout(function () {
            hasInitializedFirebase = true;
          }, 1000)
        }

      })

      onChildAdded(usersRef, async function (data) {

        if (hasInitializedFirebase) {

          console.log(data)
          
        }

      })
    </script>

</body>
</html>

That’s how you can prevent onChildAdded event to be called multiples times in Firebase. If you face any problem in this, feel free to contact me from chat widget on bottom right.