First off, it's worth pointing out that PHP is not "messing up" anything. It's escaping characters, which may look weird, but it is perfectly valid and when you json_decode it later it will be just the same as it was originally. See here: https://3v4l.org/Smj2F

If you don't like the escaping though, you can use the JSON_UNESCAPED_UNICODE flag:

https://www.php.net/function.json-encode

This flag will "encode multibyte Unicode characters literally" according to https://www.php.net/manual/en/json.constants.php.

So you can do:

json_encode($array, JSON_UNESCAPED_UNICODE);

And it will give you the following output:

["M-am întîlnit ieri cu","fosta mea profă de matematică"]

Working example: https://3v4l.org/daETG

Answer from jszobody on Stack Overflow
🌐
W3Schools
w3schools.com › js › js_json_stringify.asp
JSON.stringify()
Use the JavaScript function JSON.stringify() to convert it into a string.
🌐
Jonathan Suh
jonsuh.com › blog › convert-loop-through-json-php-javascript-arrays-objects
Convert and Loop through JSON with PHP and JavaScript Arrays/Objects — Jonathan Suh
Like JSON.parse, JSON.stringify ... JSON.stringify as well. You can combine the methods above to create powerful, dynamic implementations on your website or application. Let’s say you want to get information from a database, safely return the data as JSON, and loop through it dynamically, you can do so with a bit of PHP and JavaScript with ...
🌐
W3Schools
w3schools.com › js › js_json_php.asp
JSON PHP
Before you send the request to the server, convert the JSON object into a string and send it as a parameter to the url of the PHP page: Use JSON.stringify() to convert the JavaScript object into JSON:
🌐
Online Tools
onlinetools.com › json › stringify-json
Stringify JSON – Online JSON Tools
For example, it can convert a JavaScript expression, a JSON object, a string with special characters, and a larger text with newlines into a compactly stringified JSON string. It works in the browser and uses the native JSON.stringify() function to get the job done.
Find elsewhere
🌐
JUCE
forum.juce.com › feature requests
FR: change JSON::toString output to match php & javascript json_encode - Feature Requests - JUCE
December 9, 2022 - Php’s json_encode produces json-formatted text that does not have a space after the ":". JUCE’s JSON::toString does. It happens here in void DynamicObject::writeAsJSON (OutputStream& out, const int indentLevel, const b…
🌐
PHPpot
phppot.com › php › json-handling-with-php-how-to-encode-write-parse-decode-and-convert
JSON Handling with PHP: How to Encode, Write, Parse, Decode and Convert - PHPpot
This is the reverse process of the above section. JavaScript supports this conversion by using its built-in JSON.stringify method.
Top answer
1 of 2
1

Change the Python script so it prints JSON instead of Python format.

print(json.dumps(out))

However, sets aren't in JSON, so change outnews to a list.

outnews = [html.unescape(currentNews["timestamp"]), html.unescape(currentNews["title"]), html.unescape(currentNews["description"]), html.unescape(currentNews["link"])]

Then the PHP script can simply return that to the client.

    if ($_GET['times'] == 0) {
        $command = escapeshellcmd('python3 feed.py '. $_GET['subject']);
        header('Content-type: application/json');
        passthru($command);
    }

If passthru() isn't working, you can try with your original shell_exec(). You don't need to call json_encode() because it's already encoded.

    if ($_GET['times'] == 0) {
        $command = escapeshellcmd('python3 feed.py '. $_GET['subject']);
        $output = shell_exec($command);
        
        header('Content-type: application/json');
        echo $output;
    }
2 of 2
0

Also If I want to get back all news than change out and return it back like this:

out = []

error = 0
status = 0
nrOfResults = 0

if requestpost.status_code == 200:
    status = 200
    for news in response_data["news"]:
        try:
            currentNews = json.loads(news)
            if ((html.unescape(currentNews["title"]) != "Array" and html.unescape(currentNews["title"]).lower().find(searchterm.lower()) != -1) or (html.unescape(currentNews["description"]).lower().find(searchterm.lower()) != -1)):         
                outnews = [html.unescape(currentNews["timestamp"]), html.unescape(currentNews["title"]), html.unescape(currentNews["description"]), html.unescape(currentNews["link"])]
                out.append(outnews)
                nrOfResults = nrOfResults +1
        except:
            error += 1
else:
    status = 404

out.append(status)
out.append(nrOfResults)

#outnews = [html.unescape(currentNews["timestamp"]), html.unescape(currentNews["title"]), html.unescape(currentNews["description"]), html.unescape(currentNews["link"])]
print(json.dumps(out))

than the last element of js array will be a the number of results and the before one will be the status of the source link.

🌐
Dyn-web
dyn-web.com › tutorials › php-js › json › stringify.php
Using JavaScript's JSON.stringify
JavaScript · PHP · Site Map · The JSON.stringify method converts a JavaScript value into a JSON string. It is typically used to convert JavaScript arrays or objects to JSON, although it can also be used with simple data types like strings and numbers.
🌐
PHP
php.net › manual › en › function.json-encode.php
PHP: json_encode - Manual
It will create a JSON-encoded string without throwing an error, but this string will cause an error when parsed. For example, the string "Example\" will cause the following error in JavaScript when parsing: JSON.parse: expected ',' or '}' after property value in object This appears to be an ...
🌐
GitHub
gist.github.com › merin83 › c0460e90335a8d91ce4894bd0776a8d2
Decode JSON.stringify(object) in php · GitHub
Decode JSON.stringify(object) in php. GitHub Gist: instantly share code, notes, and snippets.
🌐
Dyn-web
dyn-web.com › tutorials › php-js › json › array.php
Pass PHP Arrays to JSON and JavaScript Using json_encode
The PHP json_encode function translates the data passed to it to a JSON string which can then be output to a JavaScript variable. We demonstrate on this page with single level arrays.