0

How do I convert JSON-LD extracted from website to a PHP array.

This is the Error I'm getting with the following code---

"Warning: Illegal string offset 'name' in /Users/lightingsystem/sigclub/signatureclub/public/json/json2php.php on line 12 {"

--code--

$json = '{ "@context": "http://schema.org", "@type": "LodgingBusiness", "name":"Breakerwood Suites", "amenityFeature":"A nice place to sleep", "audience":{ "@type":"Audience", "audienceType":"All" }, "checkinTime":"16:00", "checkoutTime":"10:00", "petsAllowed":"No", "availableLanguage":"", "openingHours":"", "paymentAccepted":"", "address":"2323 Bowers St.,New Orleans,LA,USA,69969", "image": "https://www.picturebook.com/resort2.jpg", "telephone":"Resort telephone number: 504/339-4543" }';

$coded = json_encode($json);

$data = json_decode($coded, true);

echo $data['name'];

1 Answer 1

2

Your call to json_encode is wrong because $json is already in the JSON format.

So what happens is: your JSON gets converted to JSON again, returning a JSON string.

Remove the json_encode call and your code works as expected.

<?php

$json = '{ "@context": "http://schema.org", "@type": "LodgingBusiness", "name":"Breakerwood Suites", "amenityFeature":"A nice place to sleep", "audience":{ "@type":"Audience", "audienceType":"All" }, "checkinTime":"16:00", "checkoutTime":"10:00", "petsAllowed":"No", "availableLanguage":"", "openingHours":"", "paymentAccepted":"", "address":"2323 Bowers St.,New Orleans,LA,USA,69969", "image": "https://www.picturebook.com/resort2.jpg", "telephone":"Resort telephone number: 504/339-4543" }';

$data = json_decode($json, true);

echo $data['name'];

Note:

Sign up to request clarification or add additional context in comments.

1 Comment

Great! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.