php get values from json encode

php get values from json encode

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Parsing JSON Encoded GET Parameters in PHP

You have successfully serialized your incoming $_GET parameters into a JSON string using json_encode. The resulting output, such as {"countryId":"84","productId":"1"}, is valid JSON. However, simply echoing this string does not automatically make it available for further processing within your current request lifecycle. To manipulate these values, you must reverse the process by decoding the string back into a native PHP array using json_decode.

The core mechanism relies on understanding how json_decode handles data types. By default, when you pass a JSON string to this function, it returns a stdClass object rather than an associative array. While objects work well for iterating over properties using dot notation (e.g., $data->countryId), they are less intuitive for accessing multiple values or passing them into other functions that expect arrays.

To convert the decoded result into an array, you must pass a second argument of true to json_decode. This forces the function to return an associative array instead of an object. Here is how you would implement this in your script:

$jsonString = '{"countryId":"84","productId":"1","status":"0","opId":"134"}';
$data = json_decode($jsonString, true);

// Access values using bracket notation or dot notation on the object version
$countryId = $data['countryId']; 
$productId = $data['productId'];
$status    = $data['status'];
$opId      = $data['opId'];

This approach allows you to extract specific fields for validation, database queries, or business logic. You can immediately use $countryId in a query like SELECT * FROM products WHERE country_id = ? without needing to cast the variable again.

Handling Type Safety and Null Values

A critical pitfall when using json_decode is handling cases where keys are missing or values are null. If the incoming JSON string omits a key, json_decode will return null for that specific index in the array. This can break logic if you assume every variable holds an integer or string.

Always check if the decoded value exists before processing it. Using the coalesce operator or explicit checks ensures your application doesn't crash on malformed input:

$countryId = $data['countryId'] ?? null;

if ($countryId !== null) {
    // Proceed with logic requiring countryId
}

Additionally, consider that json_encode defaults to escaping characters and treating all values as strings. When you decode them back, they retain those string types unless your original PHP array contained native integers or booleans and the JSON encoder was configured differently (which is rare in standard $_GET scenarios). If your downstream logic expects integers for arithmetic operations or strict type comparisons, remember to cast the values explicitly:

$numericId = (int) $data['productId'];
$statusBool = ($status === '1');

Why Not Just Use $_GET?

In this specific scenario where you are encoding $_GET parameters only to immediately decode them, it is technically redundant. The raw $_GET superglobal already contains the associative array of values passed via the URL query string. Using json_encode adds a layer of serialization overhead and complexity without providing inherent benefits for simple parameter access.

However, this pattern becomes relevant when you need to pass parameters as a single blob to another system, or when working with APIs that strictly require JSON payloads in the body rather than query strings. In those cases, encoding is necessary. If you are doing so, json_decode is the required companion function to reconstruct your data model for internal use.

For developers building robust Laravel applications, managing input data often involves decoupling raw request inputs from application logic. While handling raw JSON decoding is a valid skill, frameworks like Laravel provide helpers and validation rules that abstract away these manual steps. You might find it useful to explore how LaravelCompany approaches input sanitization and form handling within the broader ecosystem of modern PHP development, ensuring you are leveraging built-in tools for complex scenarios rather than managing raw strings manually.

Our network