2

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?

6
  • What would need to be done to return each JSON object separately? Right now it prints each one individually with a newline between each one. What more do you want? Commented Dec 16, 2021 at 13:08
  • 2
    Why don't you return a JSON array? [{"key": "somevalue", "assignee": "othervalue"}, {"key": "value", "assignee": "anothervalue"}]. Then you can json_decode it just fine. Commented Dec 16, 2021 at 13:12
  • @mark_b thanks for the suggestion, see the updated field, the format is an array now, but it's as a string still, how can I go from string->array Commented Dec 16, 2021 at 13:21
  • don't post pictures of output, post as text please. Commented Dec 16, 2021 at 13:54
  • @berend it's changed now Commented Dec 16, 2021 at 14:02

2 Answers 2

1

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>";
}
Sign up to request clarification or add additional context in comments.

Comments

0

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).

1 Comment

Thanks for the suggestion, but I found a solution for my problem, by solving is over in the python code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.