0

How to decode this array of object to Php array, I used json_decode() but that returned Null

$a = "[
{
    id:1,
    name:'rajan',
    class:10
},{
    id:2,
    name:'amrit',
    class:12
},{
    id:3,
    name:'arun',
    class:11
}
]";
Rajan
  • 1,463
  • 12
  • 26
  • Welcome to Stack Overflow! This question is a little short on information. Can you share what you have tried, and what problems have you run into? You have a string right now - so it is not decodable. – Jay Blanchard Jan 29 '15 at 14:47
  • 1
    The lack of quote marks around key values such as `id`, `name` and `class` suggests that this is not valid json – Mark Baker Jan 29 '15 at 14:52
  • I tried to decode above string using json_decode, but that does not worked out!! I need to know how can I convert above string to php array – Rajan Jan 29 '15 at 14:55
  • @MarkBaker Yes, I know there is no quotes on the key of each objects, that's the reason I am getting null on the json_decode but the key of the json object may not be Quoted thats doesnot mean thats invalid json!!! am I right?? I have to decode in such case :( I spend couple of hour on it!! – Rajan Jan 29 '15 at 14:59
  • So where is this json coming from? The optimal solution is to fix it at source, not to put in some workround/monkeypatch to resolve it.... and RFC4627 is quite clear about the need for quote marks: `The representation of strings is similar to conventions used in the C family of programming languages. __A string begins and ends with quotation marks.__` – Mark Baker Jan 29 '15 at 15:19
  • An alternative might be a preg_replace that searches for unquoted strings before a `:` and wraps them in quotes before you do your json_decode – Mark Baker Jan 29 '15 at 15:23
  • @MarkBaker What's I posted is array of JavaScript Object, and that's why I was unable to use, json_decode!! I was wrong saying it JSON data, perhaps i should have told you array of javascript object What will be the easiest way to make it php array then!!! – Rajan Jan 29 '15 at 15:29
  • So you are starting out with a string of Javascript that you'd like php to parse? is it possible to have the source of this javascript string convert it to JSON using JSON.stringify() ? – khartnett Jan 29 '15 at 15:51
  • @khartnett I don't have access to that!! – Rajan Jan 29 '15 at 16:04
  • `$.ajax({ url: '{{ base_url('home/data_save') }}', type: 'POST', dataType: 'JSON', data: {'data':JSON.stringify(na)} }).done(function (data) { console.log(data); }).fail(function (data) { });` I just got the code, and this resulting the above data on the backend using `$req = $this->input->post('data'); print_r($req);` and the format of **na** is located on [http://jsfiddle.net/mydvzud6/](http://jsfiddle.net/mydvzud6/) – Rajan Jan 29 '15 at 16:10

1 Answers1

5

Your JSON is not valid, which is why json_decode is returning null.

SyntaxError: Unexpected token i

http://json.parser.online.fr/

http://php.net/manual/en/function.json-decode.php

Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

I have also fixed your JSON:

[
{
    "id": 1,
    "name":"rajan",
    "class":10
},{
    "id":2,
    "name":"amrit",
    "class":12
},{
    "id":3,
    "name":"arun",
    "class":11
}
]

Fixing your broken JSON with code:

$replace_keys = preg_replace("/(\w*):/i", '"$1":', $a);
$fix_values = preg_replace("/:'(\w*)'/i", ':"$1"', $replace_keys);

You can then use $replace_keys in json_decode. I am not sure if this is the best method, as small variations in your json could break it, but it fixes it with the example provided.


Full code to fix JSON:

$a = "[
{
    id:1,
    name:'rajan',
    class:10
},{
    id:2,
    name:'amrit',
    class:12
},{
    id:3,
    name:'arun',
    class:11
}
]";


$replace_keys = preg_replace("/(\w*):/i", '"$1":', $a);
$fix_values = preg_replace("/:'(\w*)'/i", ':"$1"', $replace_keys);

die($fix_values);
Jono20201
  • 3,215
  • 3
  • 20
  • 33
  • Not really an answer to his question. – Daan Jan 29 '15 at 14:49
  • @Daan Answer has been expanded. – Jono20201 Jan 29 '15 at 14:51
  • its actually its array of object, object may not have quotes around the keys!! and in the case what I supposed to do, wrap the key with the quotes and then use json_decode :( – Rajan Jan 29 '15 at 15:10
  • I know If I add the Quotes its will work, but that's not the actual solution is it??? – Rajan Jan 29 '15 at 15:11
  • Being Honest that's array of Objects :) in javascript @Jono20201 – Rajan Jan 29 '15 at 15:14
  • In pure Javascript you don't need the quotes around the keys unless using a reserved word, but when using the JSON format, it does require quotes. http://stackoverflow.com/questions/949449/json-spec-does-the-key-have-to-be-surrounded-with-quotes – khartnett Jan 29 '15 at 15:37
  • Then there is not any predefined function on php to make php array of javascript array of objects right!! @khartnett – Rajan Jan 29 '15 at 15:42
  • @Jono20201 This is Not the Exact Solution for what I have asked!! :( – Rajan Jan 29 '15 at 15:48
  • @Rajan Updated answer with a way to fix provided JSON example to 'answer the question'. – Jono20201 Jan 29 '15 at 16:24
  • @Rajan It'd be appreciated if you could 'accept' the answer if this is the full solution. – Jono20201 Jan 29 '15 at 16:29