Why is this json invalid?

0

Why this json is valid:

    {
   "query":"mutation{ createCard(input: {pipe_id: 426174 fields_attributes: [{field_id: \"o_que\", field_value: \"Igor\"}{field_id: \"idade\", field_value: \"21\"}{field_id: \"cidade\", field_value: \"Vila Velha - ES\"}] parent_ids: [\"2735966\"] }) { card {id title }}}"
}

And this is rendering invalid in the tests I do:

{"query":"mutation {
        createTableRecord(input: {
            table_id: \"RL-p4Ud_\"
            title: \"Igor Oliveira\"
            due_date: \"2018-04-10T11:04:57-03:00\"
            fields_attributes: [{
                field_id: \"nome_completo\",
                field_value: \"Igor Oliveira\"
            }, {
                field_id: \"e_mail\",
                field_value: \"[email protected]\"
            }, {
                field_id: \"telefone\",
                field_value: \"279800002\"
            }]
        }) {
            table_record {
                id title
            }
        }"}

The error is as follows:

  

Invalid characters found. [Code 18, Structure 4]

The idea is that the query has all the mutation as if it were a string.

In PHP I'm creating this:

        $query = '{"query":"';
    $query .= 'mutation {
        createTableRecord(input: {
            table_id: \"RL-p4Ud_\"
            title: \"'.$nomecompleto.'\"
            due_date: \"'.date('c').'\"
            fields_attributes: [{
                field_id: \"nome_completo\",
                field_value: \"'.$nomecompleto.'\"
            }, {
                field_id: \"e_mail\",
                field_value: \"'.$email.'\"
            }, {
                field_id: \"telefone\",
                field_value: \"'.$telefone.'\"
            }]
        }) {
            table_record {
                id title
            }
        }';
      $query .= '"}';
    
asked by anonymous 10.04.2018 / 16:16

1 answer

4

Because you broke lines inside the "..." quotes, which will invalidate the JSON, what you can do to resolve this is before using json_decode or sending to output ( application/json ) is to convert the lines in \n (and the "carts" in \r ):

For example:

$query = '{"query":"';

$query .= 'mutation {
    createTableRecord(input: {
        table_id: \"RL-p4Ud_\"
        title: \"'.$nomecompleto.'\"
        due_date: \"'.date('c').'\"
        fields_attributes: [{
            field_id: \"nome_completo\",
            field_value: \"'.$nomecompleto.'\"
        }, {
            field_id: \"e_mail\",
            field_value: \"'.$email.'\"
        }, {
            field_id: \"telefone\",
            field_value: \"'.$telefone.'\"
        }]
    }) {
        table_record {
            id title
        }
    }';

 $query .= '"}';

 $query = strtr($query, array(
      "\r" => '\r',
      "\n" => '\n'
 ));

 echo $query;

Or else you could simplify and create everything via object or array and then use json_encode , the good thing to do is that you will not even have to escape characters like " , inside your query: "..." key, the own json_encode will take care of what is necessary.

Example:

$queryObj = [
    'query' => 'mutation {
        createTableRecord(input: {
            table_id: "RL-p4Ud_"
            title: "'.$nomecompleto.'"
            due_date: "'.date('c').'"
            fields_attributes: [{
                field_id: "nome_completo",
                field_value: "'.$nomecompleto.'"
            }, {
                field_id: "e_mail",
                field_value: "'.$email.'"
            }, {
                field_id: "telefone",
                field_value: "'.$telefone.'"
            }]
        }) {
            table_record {
                id title
            }
        }'
];

$query = json_encode($queryObj);

echo $query;
    
10.04.2018 / 17:02