0

I have a bunch of raw data from work that I'd like to convert into a huge JSON so that I can extract insights from it. But this data is basically a gigantic text, and inside this JSON I would need to have some of the text into an attribute, like this:

{ "error_analysis": text_0  }

And inside text_0, I need the ENTIRE following paragraphs:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere neque risus, et congue velit scelerisque a. Proin tempus ligula tortor, nec tempus nibh blandit eget. Suspendisse potenti. Pellentesque ut nisl eu ante ornare mollis.

In eget pulvinar purus. Sed gravida, nibh eget consectetur dignissim, augue quam egestas leo, at luctus sem sem sed sapien.

Suspendisse vel massa in justo aliquam ullamcorper.

I can't just type the entire paragraph due to the end of lines, as JSON breaks into a new statement when you have a new line.

{ "error_analysis" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere neque risus, et congue velit scelerisque a. Proin tempus ligula tortor, nec tempus nibh blandit eget. Suspendisse potenti. Pellentesque ut nisl eu ante ornare mollis.

In eget pulvinar purus. Sed gravida, nibh eget consectetur dignissim, augue quam egestas leo, at luctus sem sem sed sapien.

Suspendisse vel massa in justo aliquam ullamcorper." }

So is it possible? Or do I have to manually format with \n, \t... and only then store at the JSON? Or perhaps I'll have to create a script that formats for me, if that's the fastest option, since I'm definetely not formatting manually everything :s

Thank you!

Pedro Accorsi
  • 765
  • 1
  • 8
  • 21
  • Don't generate JSON by hand. Use `json.dumps({'error_analysis': text_0})`. – chepner Feb 07 '20 at 19:49
  • Does this answer your question? [Multiline strings in JSON](https://stackoverflow.com/questions/2392766/multiline-strings-in-json) – Jesse Feb 07 '20 at 19:49

1 Answers1

2

Does this work?

text_0 = '''
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere neque risus, et congue velit scelerisque a. Proin tempus ligula tortor, nec tempus nibh blandit eget. Suspendisse potenti. Pellentesque ut nisl eu ante ornare mollis.

In eget pulvinar purus. Sed gravida, nibh eget consectetur dignissim, augue quam egestas leo, at luctus sem sem sed sapien.

Suspendisse vel massa in justo aliquam ullamcorper.
'''

import json

json.dumps({'error_analysis': text_0})
blueteeth
  • 3,330
  • 1
  • 13
  • 23