1

I have a JSON document where some of the data is multi line string

I have tried the following options and none of them seem to work

e.g.

[
  {
    "someString" : "A rather long string of English text, an error message \
               actually that just keeps going and going -- an error \
               message to make the Energizer bunny blush (right through \
               those Schwarzenegger shades)! Where was I? Oh yes, \
               you've got an error and all the extraneous whitespace is \
               just gravy.  Have a nice day."
  }
]

or

[
  {
    "someString" : `A rather long string of English text, an error message` +
               `actually that just keeps going and going -- an error` +
               `message to make the Energizer bunny blush (right through` +
               `those Schwarzenegger shades)! Where was I? Oh yes,` +
               `you've got an error and all the extraneous whitespace is` +
               `just gravy.  Have a nice day.`
  }
]

or

[
  {
    "someString" : 'A rather long string of English text, an error message' +
               'actually that just keeps going and going -- an error' +
               'message to make the Energizer bunny blush (right through' +
               'those Schwarzenegger shades)! Where was I? Oh yes,' +
               'you've got an error and all the extraneous whitespace is' +
               'just gravy.  Have a nice day.'
  }
]

or this combination with \n as suggested in the comments, but this didn't work either.

[
  {
"shortStory": "A rather long string of English text, an error message\n
           actually that just keeps going and going -- an error \n
           message to make the Energizer bunny blush (right through\n
           those Schwarzenegger shades)! Where was I? Oh yes,\n
           you've got an error and all the extraneous whitespace is\n
           just gravy.  Have a nice day."
  }
]

And various other combinations. As long as I have new line the code doesn't work.

Here is the code (Angular 2/Javascript) which reads the JSON file

   import {
        Injectable
    } from '@angular/core';
    import {
        Http,
        Headers,
        RequestOptions,
        Response
    } from '@angular/http';
    import {
        Observable,
        Subject
    } from 'rxjs/Rx';
    import 'rxjs/Rx'; //get everything from Rx
    import 'rxjs/add/operator/toPromise';
    import {
        IArticle
    } from './article';

    @Injectable()
    export class ArticleService {
        private jsonFileURL: string = "./assets/data/article-data.json";
        constructor(private http: Http) {}
        //
        getArticles(): Observable < IArticle[] > {
            return this.http.get(this.jsonFileURL).map((response: Response) => {
                return <IArticle[] > response.json()
            }).catch(this.handleError);
        }
        //
        private handleError(errorResponse: Response) {
            //console.log(errorResponse.statusText);
            return Observable.throw(errorResponse.json().error || "Server error");
        }
    }
Rajesh Jain
  • 1,159
  • 3
  • 19
  • 37
  • 3
    Because that is not valid JSON. JSON uses the escaped new line character (`\n`) for new lines. – Patrick Evans Aug 18 '17 at 18:31
  • As Patrick said, use "\n" in lieu of + or \. You should get desired output. – Oisín Foley Aug 18 '17 at 18:32
  • Not that (this won't fix your problem, but) you can use backticks as the string quotes to capture literal newlines in a string literal (although that will also capture the tabs/spaces to the left) – Stephen S Aug 18 '17 at 18:38
  • Also `{{` is invalid syntax for JSON. – Patrick Evans Aug 18 '17 at 18:40
  • sorry that was a typo, it is [{... }] – Rajesh Jain Aug 18 '17 at 18:43
  • _"with \n as suggested in the comments"_ you cannot have literal new lines in JSON, it is not valid JSON. You have to use \n and remove your literal new lines. Or if you were going for readability you will just have to remove the new lines all together, you just can't have them in it – Patrick Evans Aug 18 '17 at 18:43
  • just because this is a lot of text, doesn't mean that the string should contain line breaks. Let your IDE deal with word wrapping. If you hardcode line breaks into the string, it might interfere with a responsive layout. – Thomas Aug 18 '17 at 18:44
  • I am trying to read formatted *poems* and *articles* from json data file and which means I have to put the \n and \t in the json string. Is that the only option. – Rajesh Jain Aug 18 '17 at 18:46

1 Answers1

0

I replaced the newline literals by \n as suggested @Patrick, hopefully there is a better way to read JSON data with newlines.

[{"shortStory": "The free bird leaps\n on the back of the wind\nand floats downstream\ntill the current ends\nand dips his wings\nin the orange sun rays\nand dares to claim the sky.\n\nBut a bird that stalks\ndown his narrow cage\ncan seldom see through\nhis bars of rage\nhis wings are clipped and\nhis feet are tied\nso he opens his throat to sing.\n\nThe caged bird sings\nwith fearful trill\nof the things unknown\nbut longed for still\nand his tune is heard\non the distant hill\nfor the caged bird\nsings of freedom\n\n The free bird thinks of another breeze\n and the trade winds soft through the sighing trees\n and the fat worms waiting on a dawn-bright lawn\n\n and he names the sky his own.\n\n But a caged bird stands on the grave of dreams\n his shadow shouts on a nightmare scream\n his wings are clipped and his feet are tied\nso he opens his throat to sing\n\nThe caged bird sings\n with a fearful trill\n of things unknown\n but longed for still\n and his tune is heard\n on the distant hill\n for the caged bird \n sings of freedom"}]
Rajesh Jain
  • 1,159
  • 3
  • 19
  • 37
  • There isn't. This is the only valid encoding for newlines in JSON. I don't understand the issue, JSON isn't intended for the purpose of human readability, it's a serialized data format. – Patrick Roberts Aug 18 '17 at 18:54
  • I get it, and I am at peace with it. But from json.org "JSON (JavaScript Object Notation) is a lightweight data-interchange format. *It is easy for humans to read and write*. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999.... These properties make JSON an ideal data-interchange language." – Rajesh Jain Aug 18 '17 at 23:22
  • Well, relatively speaking to other encodings, sure. It's not a binary encoding, and property names are typically human-readable, since they're not minified, etc. But that doesn't mean _everything_ about JSON is targeted towards human readability. Its main priority is a safe format for transferring serialized data without executing arbitrary JavaScript, which is what your object notations before attempted to do, whether you realize or not. – Patrick Roberts Aug 18 '17 at 23:30