Why is this json invalid?

Asked

Viewed 309 times

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 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 for the query to have all Mutation as a single string.

In PHP I am creating like 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 .= '"}';
  • 1

    It is not possible to break a string of a json value into multiple lines. So the second example of json is invalid even.

1 answer

4


Because you broke lines inside the quotes "...", which will invalidate JSON, what you can do to resolve this is before using the json_decode or send to output (application/json) is to convert the lines into \\n (and the "strollers" 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 you could simplify and create everything via object or array and then use the json_encode, the good thing about doing this is that you won’t even need to escape characters like ", inside your key query: "...", own json_encode takes care of whatever 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;
  • 1

    That’s right, the solution I was looking for was with the creation of an object, I don’t like to do everything in one line and put that pile of exhaust

Browser other questions tagged

You are not signed in. Login or sign up in order to post.