I'm having a bit of trouble creating a JSON array in python and returning it to PHP.
Right now I have a PHP page that calls a Python script like this:
$output1 = shell_exec("cd .. && cd python/pyjira && pipenv run py PyJira/Jira.py");
var_dump($output1);
The python script creates some JSON prints
if __name__ == "__main__":
jira = Jira()
data = {}
fields = jira.get_fields()
jql_issues = jira.get_jql_search_issues(jql_search="project = SWAT AND resolution = Unresolved ORDER BY priority DESC, updated DESC")
for issue in jql_issues:
data['key'] = issue.key
data['assignee'] = issue.fields.assignee.display_name
print(json.dumps(data))
exit
The output from python
{"key": "SWAT-107", "assignee": "Unassigned"}
{"key": "SWAT-98", "assignee": "Unassigned"}
{"key": "SWAT-100", "assignee": "Unassigned"}
{"key": "SWAT-97", "assignee": "Unassigned"}
{"key": "SWAT-75", "assignee": "Unassigned"}
{"key": "SWAT-129", "assignee": "Unassigned"}
This is the var_dump(...);
from PHP, and here you can see it's multiple JSON's in a single string
"{"key": "SWAT-107", "assignee": "Unassigned"} {"key": "SWAT-98", "assignee": "Unassigned"} {"key": "SWAT-100", "assignee": "Unassigned"} {"key": "SWAT-97", "assignee": "Unassigned"} {"key": "SWAT-75", "assignee": "Unassigned"} {"key": "SWAT-129", "assignee": "Unassigned"} "
Is there a way to have python returning the JSON object one by one, so I can loop though them in PHP, and just do data['key']
etc.?
I know when I just have one of the JSON's from the outout like {"key": "SWAT-107", "assignee": "Unassigned"}
the I just need to json_decode(...)_;
it in PHP.
Update
As the comment suggested, I now tried to return a array from python, and get this on the PHP site:
"['{"key": "SWAT-106", "assignee": "Unassigned"}', '{"key": "SWAT-107", "assignee": "Unassigned"}', '{"key": "SWAT-98", "assignee": "Unassigned"}', '{"key": "SWAT-100", "assignee": "Unassigned"}', '{"key": "SWAT-97", "assignee": "Unassigned"}', '{"key": "SWAT-75", "assignee": "Unassigned"}', '{"key": "SWAT-129", "assignee": "Unassigned"}'] "
How can I make it into a array?
CodePudding user response:
For the string before the update you posted I came up with this kinda messy solution:
$string = '{"test1": "1", "test2": "2"} {"test3": "3", "test4": "4"} {"test5": "5", "test6": "6"}';
$array = explode(" {", $string);
foreach ($array as $k => $v){
if(substr($v, 0, 1) != "{"){
$array[$k] = "{" . $v;
}
}
$finalArray = [];
foreach($array as $v) {
$finalArray[] = json_decode($v);
}
I did it on my test $string which is simillar to yours and it creates me an array of objects (the $finalArray).
CodePudding user response:
The solution was more simple than I thought
If we start with the python, this is where the issue is.
First (as suggested from the comment) I put all the JSON data into a array, useing json.dump()
but then outside the loop I print the array with json.dump()
, this makes PHP, know that the printet variable is a JSON.
if __name__ == "__main__":
jira = Jira()
data = {}
output = []
fields = jira.get_fields()
jql_issues = jira.get_jql_search_issues(jql_search="project = SWAT AND resolution = Unresolved ORDER BY priority DESC, updated DESC")
for issue in jql_issues:
data['key'] = issue.key
data['assignee'] = issue.fields.assignee.display_name
output.append(json.dumps(data))
print(json.dumps(output))
exit
Over in my PHP I simply call the python script and fetch the JSON, then decode it. Then I have the array, and to get the key
and assignee
I have to decode it, and set assosiative = TRUE
to convert object to array.
$output1 = shell_exec("cd .. && cd python/pyjira && pipenv run py PyJira/Jira.py");
// Display the list of all file
// and directory
$decoded_output = json_decode($output1);
for ($i=0; $i < count($decoded_output); $i ) {
$jira_data = json_decode($decoded_output[$i], true); // 'true' to convert object to array
echo $jira_data['key'] . " - " . $jira_data['assignee'] . "<br>";
}