I tried to parse JSON object with PHP in bash.
{
"department": {
"name": "MyDepartment",
"emp_no": 10
}
}
And
#!/bin/sh
key=department->name
jsonbody=./object.json
value=$(php -r "\$obj = json_decode(utf8_encode(file_get_contents('$jsonbody')));print \$obj->'$key';")
I got the error message as below. How can handle JSON object with bash variable?
PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or '{' or '$' in Command line code on line 1