0

While analizing some code i am using

            <script>console.log("Cias", {{ dia.cias| json_encode| raw}});</script>
            <script>console.log("Dia",{{ key| json_encode| raw}});</script>
            <script>console.log("Datos",{{ dia| json_encode| raw}});</script>
            <script>console.log("CodigoEstadoAusencia",{{ dump(dia.codEstadoAusencia)| json_encode| raw}});</script>
            <script>console.log("TipoTit",{{ dump(tipoTit)| json_encode| raw}});</script>
            <script>console.log("-------------------------------------------------------------------------");</script>

And i recieve 2 diferent outputs. This first one is incomplete and is what i want to fix

Cias 1601140113N 
Dia 22
Datos  // (empty) I also get "undefined" if i stringify the output for thiss
CodigoEstadoAusencia string(2) "30"
TipoTit string(1) "T"
-------------------------------------------------------------------------

But i also recieve correct output like the following:

Cias 1601140113N 
Dia 21
Datos
 cias: "1601140113N"​
 dia: "21"
    codEstadoAusencia: "30"​
    descripcionEstadoAusencia: "CONTRATADO"​
    estadoSustitucion: {…}​​
      pedidoSustituto: "S"
    ​​  sustitutoJano: {…}
    ​​​    CONTRATADO: "S"
    ​​​    DOBLAJE: "N"
    ​​​    FFIN: null
    ​​​    FINI: "2019-05-20"
    ​​​    ID: "213095"
    ​​​    NIF: "         "
    ​​​    NOMBRE: "John Doe"
    ​​​    SOLICITUD: "390804"​​​
        TELEFONO: "699999999           "​​​
      dia: "2019-04-21"​​
      idSolicitud: "390804"​​
    idAlta: "128096"​
    movianterior: "445223"​
    situacionAdministrativa: "T"​
    sustitutoSaint: null​
    sustitutos: "S"​
    tipoAusencia: "390804"​
    turno: "TARDE"  
CodigoEstadoAusencia string(2) "30"
TipoTit string(1) "T"
-------------------------------------------------------------------------   

As you can see in the "bad" output i can access the attributes from "dia" like dia.codEstadoAusencia where i got "30", BUT it seems unable to output the full object and no error is displayed. I've already tried the combinations of using the pipes with raw ,with/without json_encode, using json_encode(constant('JSON_PRETTY_PRINT')) , without dump, alerting the content, changing the order of dumps...

I only got the conclusion that in some cases it doesnt create the object correctly but cant figure out where is the problem. ¿Any ideas?

Edit: After the suggestion from duplicate mark i tried filtering with | convert_encoding('UTF-8', 'ISO-8859-1) and some other common charsets but the encoding doesnt seem the problem or the filter is not working for me

Edit2: Added some missing data from the response for clarification

Mbotet
  • 170
  • 2
  • 17
  • Likely some of the data contains non-ASCII characters which are not UTF-8 encoded. – deceze Oct 02 '19 at 09:23
  • Thanks for the duplicate mark but is not a php question. – Mbotet Oct 02 '19 at 09:41
  • 1
    @Mbotet of course it's a `PHP` question. As seen in the [documentation](https://twig.symfony.com/doc/2.x/filters/json_encode.html) `json_encode` uses the PHP function [`json_encode`](https://www.php.net/manual/en/function.json-encode.php) – DarkBee Oct 02 '19 at 09:52
  • @DarkBee Even if is based on php im using twig tools and im looking for a twig solution. Im working on a windows and im not considering it a windows problem. If there exists a twig tag is to look upon twig-based solutions. – Mbotet Oct 02 '19 at 10:02
  • Did you read the first comment of deceze? Verify the content u'r passing to the filter is properly `UTF-8` encoded. You should update your question with the content of `bias` – DarkBee Oct 02 '19 at 10:09
  • Of course i read that. Have you read my edit? I might be mislead on how to use the filter to try his solution (its a comment not a solution though). But my point is not to mark as duplicate just because another solution in another technology "might" work. Even if it does work it is not a duplicate and can help a lot to other users which are having trouble with twig and not using php in their proyects – Mbotet Oct 02 '19 at 10:27
  • 1
    Localise the problem better by inspecting your data properly and find something that contains diacritics or other "special characters". Those are likely not UTF-8 encoded. If in doubt dump each property manually until you find the one that won't dump. Then track back to where that data comes from and ensure you're passing it to Twig in UTF-8 encoded form. Often this means you need to set your database connection encoding properly if the data comes from the database, but that highly depends on where it's coming from. – deceze Oct 02 '19 at 10:27

0 Answers0