-1

I am using cloudinary to upload images.I am trying to decode json string generated by image upload plugin but it return null.I'm new to json plese help

Here is json string

[{"public_id":"pdf/Eagle_s_Crest_s1khaa","version":1552905253,"signature":"976a2475ba858beeb33d533b4bdd897c7730574c","width":3543,"height":5315,"format":"png","resource_type":"image","created_at":"2019-03-18T10:34:13Z","tags":[],"bytes":579592,"type":"upload","etag":"9c1ce7ee6f0d1a7ff540d9e06cfbc2bf","placeholder":false,"url":"http://res.cloudinary.com/dowh9bxad/image/upload/v1552905253/pdf/Eagle_s_Crest_s1khaa.png","secure_url":"https://res.cloudinary.com/dowh9bxad/image/upload/v1552905253/pdf/Eagle_s_Crest_s1khaa.png","access_mode":"public","existing":false,"original_filename":"Eagle ◀"
Pradeep
  • 3,093
  • 17
  • 21
  • Look at https://stackoverflow.com/questions/2410342/php-json-decode-returns-null-with-valid-json, it will help you. – Iago Calazans Mar 18 '19 at 11:16
  • 1
    Welcome to SO. Please finish the tour and you will understand [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and modify question accordingly to minimal working example. Without posting your code you risk removal of your question. With your stated trail and error code... people are more willing to help you so we both can learn. Question moved to editing. Reviewing finished. Enjoy SO ;-) – ZF007 Mar 18 '19 at 11:17
  • i think json structure is wrong i.e [{},{}], You need to end the json – M.Hemant Mar 18 '19 at 11:17
  • whats `json_last_error_msg()` say? Probably non-utf-8 `◀` `preg_replace('/[^[:print:]]/', '', $string)` – ArtisticPhoenix Mar 18 '19 at 11:18

2 Answers2

1

It is returning null because it's not valid JSON. It's missing a } and a ] on the end.

Compare here:

<?php

var_dump(json_decode('[{"public_id":"pdf/Eagle_s_Crest_s1khaa","version":1552905253,"signature":"976a2475ba858beeb33d533b4bdd897c7730574c","width":3543,"height":5315,"format":"png","resource_type":"image","created_at":"2019-03-18T10:34:13Z","tags":[],"bytes":579592,"type":"upload","etag":"9c1ce7ee6f0d1a7ff540d9e06cfbc2bf","placeholder":false,"url":"http://res.cloudinary.com/dowh9bxad/image/upload/v1552905253/pdf/Eagle_s_Crest_s1khaa.png","secure_url":"https://res.cloudinary.com/dowh9bxad/image/upload/v1552905253/pdf/Eagle_s_Crest_s1khaa.png","access_mode":"public","existing":false,"original_filename":"Eagle ◀"'));

var_dump(json_decode('[{"public_id":"pdf/Eagle_s_Crest_s1khaa","version":1552905253,"signature":"976a2475ba858beeb33d533b4bdd897c7730574c","width":3543,"height":5315,"format":"png","resource_type":"image","created_at":"2019-03-18T10:34:13Z","tags":[],"bytes":579592,"type":"upload","etag":"9c1ce7ee6f0d1a7ff540d9e06cfbc2bf","placeholder":false,"url":"http://res.cloudinary.com/dowh9bxad/image/upload/v1552905253/pdf/Eagle_s_Crest_s1khaa.png","secure_url":"https://res.cloudinary.com/dowh9bxad/image/upload/v1552905253/pdf/Eagle_s_Crest_s1khaa.png","access_mode":"public","existing":false,"original_filename":"Eagle ◀"}]'));

Either you pasted here the wrong JSON, or the upload plugin is returning invalid JSON.

Dean
  • 5,884
  • 2
  • 18
  • 24
-1

If you use json_decode , i suggest you look at the doc : http://php.net/manual/fr/function.json-decode.php Since we don't have your PHP code here i suppose that the second parameter "assoc" is not included in your code and may be the reason of your troubles

Loic H
  • 328
  • 4
  • 12