0

I am encoding my array to JSON format but it is not breaking line from the new line operator (\n).

So how to achieve this

$array = ["string" => "hello \ngood morning!!"];

$encodedJson = json_encode($array);
dd(encodedJson);

Outputs

{"string":"hello \ngood morning!!"}

But what I wanted is

{
  "string" :"hello
   good morning!!"
}

I also tried

json_encode($array,JSON_UNESCAPED_LINE_TERMINATORS);

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    _"but what i wanted is `{"string" :"hellogood morning!!"}`"_ - what, so you want the newline to completely disappear? That doesn't seem to match what you were asking for in the question title. – CBroe Apr 04 '22 at 10:50
  • yes i updated my code sorry my bad –  Apr 04 '22 at 10:50
  • A line-break in JSON is always encoded as `\n`. When you decode it, you'll see there's a linebreak there. Does this answer your question? [Are multi-line strings allowed in JSON?](https://stackoverflow.com/questions/2392766/are-multi-line-strings-allowed-in-json) – Markus AO Apr 04 '22 at 10:51
  • why you want the carriage return in the json string? what the advantage instead of \n? – Andrea_86 Apr 04 '22 at 10:51
  • What you are asking for makes no sense to begin with, because that would be _invalid_ JSON. You can not have actual line breaks in JavaScript text literals delimited with double or single quotes. – CBroe Apr 04 '22 at 10:51
  • This appears to still be directly related to https://stackoverflow.com/q/71732900/1427878, so you should have continued the discussion there, rather than ask a new question. – CBroe Apr 04 '22 at 10:53
  • @CBroe: I think the new question was much better than the old one. Much more succinct. Perhaps it is better to delete the old question. – KIKO Software Apr 04 '22 at 10:56
  • No, if you want valid JSON. Please search and find any number of existing questions and answers on this, e.g. [Why is JSON_UNESCAPED_LINE_TERMINATORS not unescaping my newlines?](https://stackoverflow.com/questions/64238947/why-is-json-unescaped-line-terminators-not-unescaping-my-newlines). – Markus AO Apr 04 '22 at 10:56
  • The best you could do as far as valid JSON is: `{ "string" : [ "hello", "good morning!!" ] }`, and then pretty-print that, ie. split your string into an array. And then `join` when you output. If you must have the line-breaks that is. – Markus AO Apr 04 '22 at 10:58
  • i checked this question but i thought it was not helping me –  Apr 04 '22 at 10:58
  • 1
    The question/answer above also notes, **unencoded newlines inside a string are not valid JSON**. Also the earlier question I linked has an answer with reference to JSON spec and notes, **Newlines are "control characters" so, no, you may not have a literal newline within your string.**, and also **JSON does not allow "real" newlines in its data; it can only have escaped newlines.**. Knowing that "it's not possible" should be helpful, don't chase for tricks that evade specification. If you need a particular display that's not supported "raw", find an IDE that provides it. – Markus AO Apr 04 '22 at 11:02
  • so what i am looking is irrelevant and illogical according to you i guess? –  Apr 04 '22 at 11:09

1 Answers1

6

What you want is not valid JSON.

The JSON data format does not support literal new lines in the middle of strings.

There is no way to make json_encode produce that output (and if you could, you then couldn't parse the result in any standard JSON parser).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335