A
A
Aidar2018-05-09 16:18:01
PHP
Aidar, 2018-05-09 16:18:01

How to remove quotes in json file output using php?

Hello! For a sample, let's take a json file with the following content:
{ "name": "Вася", "age": "35"}
Let's say this file has been read and now before writing back, you need to remove quotes from Vasya and age
How can this be done?

$json = json_decode(file_get_contents('test.json'), true); //Читаю файл так
----------
file_put_contents('test.json', json_encode($json, JSON_UNESCAPED_UNICODE)); //Пишу файл так

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Melkij, 2018-05-09
@melkij

you need to remove the quotes from Vasya

By default , strings must be in quotes. Therefore, you want to get non-JSON, respectively, write your own handler for your format, following the grammar of the format that you need. But don't call it json.

B
BitRouter, 2018-05-09
@BitRouter

syntactically it is not correct
Vasya - string
35 - integer (it can be done without brackets intval(35))

M
McBernar, 2018-05-09
@McBernar

Json cannot be without quotes, sorry. If you are going to use the built-in API of the language, then this will not work in any way. If you just save your pseudo-json - in a txt file or as a line in the database - cut whatever you want. I think removing the quote from the string will not be a problem for you.

Y
Yan-s, 2018-05-09
@Yan-s

Add JSON_NUMERIC_CHECK to encode numbers as numbers (i.e. without quotes) and not as strings

json_encode($json, JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question