2

I've thousands of javascript object formatted text file (not JSON) like following format; Since they're not json, json_decode function does not work with them. Googling was not very helpful, while every article I found was about PHP to JSO :(

{
guid:205, 
      data:{something:"value",
            anotherthing :0,
            thingy: "another value"},
      anotherdata:[[563, "value", 1],
            [564, "value2", 5],
            [565, "value3", 125]],
      onemore:["key", "value"]
}

I need to reach this values with PHP, but couldn't find a solution :(

$str = ??? /any function, class etc.
echo $str->data->thingy; // another value;

or

echo $str['anotherdata'][2][1] // value3;

Any idea?

Turcia
  • 653
  • 1
  • 12
  • 29
  • By looking at similar problems. It's not a problem that can be simply solved with a regex. http://stackoverflow.com/questions/4843746/regular-expression-to-add-double-quotes-around-keys-in-javascript – Rok Burgar Apr 27 '13 at 11:35
  • How is the js code you gave not json? – Achshar Apr 27 '13 at 11:40
  • Ok maybe it's the `[]` array notation? – Achshar Apr 27 '13 at 11:40
  • @Anchshar, it might be look like json code, actually I thought that it's json for 4-5 hours :), but it's not valid json. So that's the problem. I have no chance to edit it, too. :( – Turcia Apr 27 '13 at 13:20
  • possible duplicate of [PHP reading invalid json with json\_decode();](http://stackoverflow.com/questions/11729051/php-reading-invalid-json-with-json-decode) – Paulo Scardine Apr 27 '13 at 22:14

2 Answers2

4

Maybe you could parse the plain text in order to fix the string adding the missed double-quotes in the object keys. Something like:

$str = '{
guid:205, 
  data:{something:"value",
        anotherthing :0,
        thingy: "another value"},
  anotherdata:[[563, "value", 1],
        [564, "value2", 5],
        [565, "value3", 125]],
  onemore:["key", "value"]
}';

$str = preg_replace('/(\w+)\s{0,1}:/', '"\1":', str_replace(array("\r\n", "\r", "\n", "\t"), "", $str));
$array_data = json_decode($str);

That would fix the JSON malformed string and you'd be able to decode it. The bad thing could be in case some of the values of the elements contain a text with a word followed by a colon (i.e. whateverword:)

Uncledrella
  • 111
  • 4
-5

Do you aware of that your example text is a kind of JSON String? You can able to convert by json_decode.

Please see http://php.net/json_decode

  • Did you read the beginning of the question? *"I've thousands of javascript object formatted text file (not JSON) like following format; Since they're not json, `json_decode` function does not work with them."* – Felix Kling Apr 27 '13 at 10:59
  • 1
    [The `json_decode` documentation](http://php.net/json_decode) says that the JSON must have its object keys double-quoted. Sorry, but `json_decode` won't work. – michaelb958--GoFundMonica Apr 27 '13 at 10:59
  • @FelixKling Could you explain the differences between JSON string and javascript object formatted text file??? – Cagdas Emek Apr 27 '13 at 11:25
  • As michaelb958 already said, keys must be strings (i.e. in double quotes) to make it valid JSON. – Felix Kling Apr 27 '13 at 12:02