Jump to content

JSON Multi-Line String


Recommended Posts

“JSON is a popular data-interchange format that has been widely adopted across the development community. However, JSON has strict schema and formatting tools.

JSON does not natively support multi-line strings. This can be quite frustrating when looking for flexibility and storing large texts.

Let us discuss a simple workaround and how we can use multiline strings.”

Using a JSON Array

A creative way of inserting a multi-line string in JSON is to use an array. We can separate each line as an individual element of the array.

For example, let us say we have a sample text as shown:

Lorem ipsum dolor sit amet. Aut eveniet sequi sit sequi dicta vel impedit architecto 33 voluptas tempore et optio libero qui molestiae possimus. Aut cupiditate animi ut commodi natus ut nesciunt mollitia ea ipsam iusto a ipsa odit a laboriosam neque et vero totam.

If we want to convert the above block of text into JSON, we can convert each line into an array element.

For example:

    [
        "Lorem ipsum dolor sit amet.",
        "Aut eveniet sequi sit sequi dicta vel impedit architecto,",
        "33 voluptas tempore et optio libero qui molestiae possimus.",
        "Aut cupiditate animi ut commodi natus ut nesciunt mollitia ea,",
        "ipsam iusto a ipsa odit a laboriosam neque et vero totam."
    ]

Note how each line of the above string is converted into an array element separated by a comma.

You can then use a method in your preferred programming language to construct the string back to its individual block.

Conclusion

This tutorial provides a simple but creative way of creating a multi-line string in JSON.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...