1

I have this JSON object:

{
  "type": "FeatureCollection",
  "features": [
    {
      "id": "0eb3d8a7d9afa466766e90b47f2bd785",
      "type": "Feature",
      "properties": {
        "feature-description": "<div id=\"content\">html content here</div>"
      },
      "geometry": {
        "coordinates": [
          [
            [
              144.87452272951856,
              -37.72702750630904
            ],
            [
              145.00292543948336,
              -37.78131678501029
            ],
            [
              145.12926821291518,
              -37.732458226660285
            ],
            [
              145.11278872073137,
              -37.70801684893612
            ],
            [
              144.9184680420177,
              -37.72431199679091
            ],
            [
              144.87452272951856,
              -37.72702750630904
            ]
          ]
        ],
        "type": "Polygon"
      }
    }
  ]
}

If I JSON.stringify it and try to decode in PHP:

$jsonString = '"{\n  \"type\": \"FeatureCollection\",\n  \"features\": [\n    {\n      \"id\": \"0eb3d8a7d9afa466766e90b47f2bd785\",\n      \"type\": \"Feature\",\n      \"properties\": {\n        \"feature-description\": \"<div id=\"content\">html content here</div>\"\n      },\n      \"geometry\": {\n        \"coordinates\": [\n          [\n            [\n              144.87452272951856,\n              -37.72702750630904\n            ],\n            [\n              145.00292543948336,\n              -37.78131678501029\n            ],\n            [\n              145.12926821291518,\n              -37.732458226660285\n            ],\n            [\n              145.11278872073137,\n              -37.70801684893612\n            ],\n            [\n              144.9184680420177,\n              -37.72431199679091\n            ],\n            [\n              144.87452272951856,\n              -37.72702750630904\n            ]\n          ]\n        ],\n        \"type\": \"Polygon\"\n      }\n    }\n  ]\n}"';

$json = json_decode( $jsonString );
echo gettype($json); // string

I still get a string, instead of an object. What am I doing wrong?

eozzy
  • 66,048
  • 104
  • 272
  • 428
  • 1
    For future reference you can use json_last_error() to return the last error that occurred. https://www.php.net/manual/en/function.json-last-error.php – jwitt98 May 15 '19 at 23:38
  • Possible duplicate of [How can I parse a JSON file with PHP?](https://stackoverflow.com/questions/4343596/how-can-i-parse-a-json-file-with-php) – miken32 May 16 '19 at 03:39
  • "The JSON.stringify() method converts a JavaScript object or value to a JSON string..." https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify in other words it creates JSON. You would not run it on already-existing JSON or you end up with what you've got there. – miken32 May 16 '19 at 03:42

2 Answers2

1

You just added an extra double quotes " at the start and end. Just remove those and it'll work as you expected.

<?php
$jsonString = '{"type":"FeatureCollection","features":[{"id":"0eb3d8a7d9afa466766e90b47f2bd785","type":"Feature","properties":{"feature-description":"<div id=\"content\">html content here</div>"},"geometry":{"coordinates":[[[144.87452272951856,-37.72702750630904],[145.00292543948336,-37.78131678501029],[145.12926821291518,-37.732458226660285],[145.11278872073137,-37.70801684893612],[144.9184680420177,-37.72431199679091],[144.87452272951856,-37.72702750630904]]],"type":"Polygon"}}]}';
$json = json_decode( $jsonString );
echo gettype($json); // object

WORKING DEMO: https://3v4l.org/N56DS

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

You may not want to stringify it. You can just pass it through the json_decode, and it would work:

$jsonString = '{
  "type": "FeatureCollection",
  "features": [
    {
      "id": "0eb3d8a7d9afa466766e90b47f2bd785",
      "type": "Feature",
      "properties": {
        "feature-description": "<div id=\"content\">html content here</div>"
      },
      "geometry": {
        "coordinates": [
          [
            [
              144.87452272951856,
              -37.72702750630904
            ],
            [
              145.00292543948336,
              -37.78131678501029
            ],
            [
              145.12926821291518,
              -37.732458226660285
            ],
            [
              145.11278872073137,
              -37.70801684893612
            ],
            [
              144.9184680420177,
              -37.72431199679091
            ],
            [
              144.87452272951856,
              -37.72702750630904
            ]
          ]
        ],
        "type": "Polygon"
      }
    }
  ]
}';

$json = json_decode($jsonString, true);
echo gettype($json); // string

var_dump($json);

Output

array(2) {
  ["type"]=>
  string(17) "FeatureCollection"
  ["features"]=>
  array(1) {
    [0]=>
    array(4) {
      ["id"]=>
      string(32) "0eb3d8a7d9afa466766e90b47f2bd785"
      ["type"]=>
      string(7) "Feature"
      ["properties"]=>
      array(1) {
        ["feature-description"]=>
        string(41) "<div id="content">html content here</div>"
      }
      ["geometry"]=>
      array(2) {
        ["coordinates"]=>
        array(1) {
          [0]=>
          array(6) {
            [0]=>
            array(2) {
              [0]=>
              float(144.87452272952)
              [1]=>
              float(-37.727027506309)
            }
            [1]=>
            array(2) {
              [0]=>
              float(145.00292543948)
              [1]=>
              float(-37.78131678501)
            }
            [2]=>
            array(2) {
              [0]=>
              float(145.12926821292)
              [1]=>
              float(-37.73245822666)
            }
            [3]=>
            array(2) {
              [0]=>
              float(145.11278872073)
              [1]=>
              float(-37.708016848936)
            }
            [4]=>
            array(2) {
              [0]=>
              float(144.91846804202)
              [1]=>
              float(-37.724311996791)
            }
            [5]=>
            array(2) {
              [0]=>
              float(144.87452272952)
              [1]=>
              float(-37.727027506309)
            }
          }
        }
        ["type"]=>
        string(7) "Polygon"
      }
    }
  }
}
Emma
  • 27,428
  • 11
  • 44
  • 69