adhocore / php-json-fixer

Fix truncated JSON data
MIT License
49 stars 7 forks source link

Trailing comma results in Could not fix JSON (tried padding ``) #14

Closed ignacio-dev closed 11 months ago

ignacio-dev commented 1 year ago

When I try fixing this variable:

$json =
"{
    \"names_ingredients\": true,
    \"edible_ingredients\": [\"chicken\", \"cream\", \"pasta\"],
    \"inedible_items\": [],
    \"steps\": [
        \"Cook pasta according to package instructions.\",
        \"Cut chicken into small pieces and cook in a pan until browned.\",
        \"Add cream to the pan and cook until it starts to thicken.\",
        \"Add cooked pasta to the pan and stir until well combined.\",
        \"Serve hot.\",
    ],
    \"title\": \"Creamy Chicken Pasta\"
    }
";

It throws this exception:

Could not fix JSON (tried padding ``)

If I remove the trailing comma it works, but I thought this library was capable of removing trailing commas on my json?

adhocore commented 1 year ago

trailing comma is supported only at the end, the remaining left side of json substring itself must be valid json. if you are looking for comma, comments etc anywhere in json string https://github.com/adhocore/json-comment will do that.

ignacio-dev commented 1 year ago

Thank you for pointing to an alternative :)