15

I have a php script , it accessing a json file by using file_get_contents(), inside a json file, we have declared a php variable. Please let me know is there any way to parse the php variable inside a json file.

Below is the code: test.json

{
    "result":"$result",
    "count":3
}

php script for accessing the json file

<?php
$result = 'Hello';
$event = file_get_contents('test.json');
echo $event;
?>

Now the output is like below:

{ "result":"$result", "count":3 } 

But I need the output like this

{ "result":"Hello", "count":3 } 

I'm not able to access the $result variable inside a json file. Any help Appreciated.Thanks

Vipin
  • 847
  • 1
  • 10
  • 21
  • What do you mean by accessing the `$result` variable?! Please provide a sample of what you want. – someOne Jun 29 '15 at 04:27
  • I just googled your query I found this. Not sure but you might get some help http://stackoverflow.com/questions/4343596/parsing-json-file-with-php – Avnish Mehta Jun 29 '15 at 04:37
  • 4
    Your questions is really *"how can I interpolate an existing string literal?"* or something like that – Phil Jun 29 '15 at 04:37
  • Don't forget to JSON-escape your `$result` string! – Bergi Jun 29 '15 at 13:31

4 Answers4

15

I might get downvoted for this, but could eval be used in this situation?

<?php

$json = '{
    "result":"$result",
    "count":3
}'; //replace with file_get_contents("test.json");

$result = 'Hello world';



$test = eval('return "' . addslashes($json) . '";');
echo $test;

Output:

{
    "result":"Hello world",
    "count":3
}
Dave Chen
  • 10,887
  • 8
  • 39
  • 67
  • It's the only thing I can think of at the moment as well – Phil Jun 29 '15 at 04:43
  • 2
    @Phil It's either that, or make up your own parsing scheme (which would be much better than eval IMO). – Dave Chen Jun 29 '15 at 04:44
  • 2
    Just keep in mind that PHP will interpolate everything starting with `$` and the results may not even be valid JSON anymore. – Ja͢ck Jun 29 '15 at 10:22
9

It's not hard to interpolate this when you parse the structure first; you could then use array_walk() to iterate over each element in the array and change the value if it matches something that starts with $:

$json=<<<'JSON'
{
    "result":"$result",
    "count":3
}
JSON;

// parse data structure (array)
$data = json_decode($json, true);

// define replacement context
$context = ['$result' => 'Hello'];
// iterate over each element
array_walk($data, function(&$value) use ($context) {
    // match value against context
    if (array_key_exists($value, $context)) {
        // replace value with context
        $value = $context[$value];
    }
});

echo json_encode($data); // {"result":"Hello","count":3}

The obvious advantage is that you don't have to worry about escaping strings so that the JSON format is not violated.

If the data structure is recursive, you could use array_walk_recursive() instead.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
2

Instead of doing it your way like { "result":"$result", "count":3 }

I would have done like this.

I would have specified a short code for the variable for this

{ "result":"**result**", "count":3 }

and when I will get that JSON in PHP, just replace that with my desired PHP variable

$event = file_get_contents('test.json');

var $result = "hello";
$parsed_json = str_replace("**result**",  $result,$event  ); 
Umair Ayub
  • 19,358
  • 14
  • 72
  • 146
-3
<?php
$json = file_get_contents("test.json");
$json_a=json_decode($json,true);

foreach ($json_a as $key => $value){
  echo  $key . ':' . $value;
}
?>
Salman Raza
  • 320
  • 1
  • 8
  • 23