php8.5
Home/ Manual/ functions / bson/ MongoDB\BSON\fromJSON

MongoDB\BSON\fromJSON

PHP function Deprecated as of mongodb 1.20.0 Edit on GitHub ✎

(mongodb >=1.0.0)

Returns the BSON representation of a JSON value

This function has been DEPRECATED as of extension version 1.20.0 and was removed in 2.0. Applications should use MongoDB\BSON\Document::fromJSON instead.

Description

MongoDB\BSON\fromJSON(string $json): string

Converts an extended JSON string to its BSON representation.

Parameters

json (string)

JSON value to be converted.

Return Values

The serialized BSON document as a binary string.

Errors/Exceptions

  • Throws MongoDB\Driver\Exception\UnexpectedValueException if the JSON value cannot be converted to BSON (e.g. due to a syntax error).

Changelog

VersionDescription

Examples

MongoDB\BSON\fromJSON() example

php
<?php

$json = '{ "_id": { "$oid": "563143b280d2387c91807965" } }';
$bson = MongoDB\BSON\fromJSON($json);
$value = MongoDB\BSON\toPHP($bson);
var_dump($value);

?>

The above example will output:

output
object(stdClass)#2 (1) {
  ["_id"]=>
  object(MongoDB\BSON\ObjectId)#1 (1) {
    ["oid"]=>
    string(24) "563143b280d2387c91807965"
  }
}

See Also

Source: reference/mongodb/functions/bson/fromjson.xml · from the official PHP manual (php/doc-en)