How to convert lines of text to JSON Lines? - json

If you have a text file with many lines of text, is there a readily available way to convert it into the JSON Lines format?
Example text file contains:
This is the first line.
This is the "second" line.
This is the \third/ line.
This is the {fourth} line;
Example JSON Lines (.jsonl) file:
{"text": "This is the first line."}
{"text": "This is the \"second\" line."}
{"text": "This is the \\third\/ line."}
{"text": "This is the {fourth} line;"}
I was hoping there is a simple way to sort of linearly transform it like this, while escaping the special characters for JSON. Are there online or (CLI) tools for the Mac that can do this?

A browser is the most handy way to access an interpreter nowadays. Here's a quick solution based on that:
Copy and paste your lines into this page: https://codebeautify.org/javascript-escape-unescape
Then copy the escaped string, press F12 on a modern broser and paste the following code:
console.log(JSON.stringify("***PASTE HERE**".split("\n").map(x => ({text: x}))))
Then delete ***PASTE HERE*** and paste the escaped lines. Press enter and you'll have a JSON output similar to what you want.

Related

Extract and Create Property in JSON file with RegEx

I have the following JSON file. Dotted across the file is the following:
"properties": {
"Name": "Darlington",
"Description": "<br><br><br> <table border=\"1\" padding=\"0\"> <tr><td>CCGcode</td><td>00C</td></tr> <tr><td>CCGname_short</td><td>Darlington</td></tr>"
}
Using RegEx, I would like to extract the CCG Code property and add it back in so that the above becomes:
"properties": {
"Name": "Darlington",
"CCGcode": "00C",
"Description": "<br><br><br> <table border=\"1\" padding=\"0\"> <tr><td>CCGcode</td><td>00C</td></tr> <tr><td>CCGname_short</td><td>Darlington</td></tr>"
}
I've tried all sorts and I just can't get it to work. I am using Sublime Text.
^("Description":").*?<td>CCGcode<\/td><td>([^<>\n]*).*$
The above selects the code, but not sure how I can get it to create the property.
Try this
( *)"Description".*?CCGcode.*?<td>([^<]+)
Regex demo
This one for sublimetext3
Find what:
( *)("Description".*?CCGcode.*?<td>)([^<]+)
Replace with:
\1"CCGcode": "\3",\n\1\2
Demo
There's a very simpel, but not so elegant, solution. Replace
"Description":.*?<td>CCGcode<\/td><td>([A-Z\d]*)<\/td>
with
"CCGCode":"\1",\n \0
Don't know how Sublime handles replacements, but you may have to change the replacing \0 and \1 to something else - e.g. $0 and $1.
What it does is to find the Description entry and the following CCGCode entry, capturing the code into capture group 1.
Then replace capture group 0 - the whole matched text, with the new CCGCode JSON tag plus the original text.
It's a pretty fragile solution, but it works for your sample case.
Check out example at regex101.
Regards

JSON string data display with breaks?

So I'm creating a game for work, which grabs question and answer data from a JSON file that someone helped me create. All I want to figure out, is how to make the string that is returned from the data below display with line breaks for each of the multiple choice answers:
{
"question": "1",
"text" : "How many times has the Actuarial computer lab been moved? A. Once B. Twice C. Six times D. Fifteen times",
"answer" : "1,1"
},
I've been googling for a while (lots of Stackoverflow threads) but every solution appears to be something different or slightly more complex than what I want to do.
Here is how I'm displaying the string into my div:
var displayDiv = $("#textdisplay");
displayDiv.text(question.text);
You probably want to add <br/> or <p> tags to your text in the JSON file itself. If you've got a text editor that can do regex string replacement, then you want to do something like
Find: ([ABCD]\.\s)
Replace: <br/>$1
Or if you're on a machine with sed, you can use
sed -E 's/([ABCD]\. )/<br\/>\1/g' test.txt
That'd be my recommendation, at least. If you can't change the JSON file, use the same Regex to add breaks in JavaScript, like so:
question.text.replace(/([ABCD]\. )/g, '<br/>$1')

Multiline Text Literal in 4D

Is it possible to enter a multiline text/string literal in a 4D method? Something like:
C_TEXT($var)
$var:="""First Line
Second Line
Third Line"""
Using the triple-" here as they are being used in Python, just as example.
You could also do it that way in 4D v12 and above
C_TEXT($var)
$var:="First Line\r"+\
"Second Line\r"+\
"Third Line"
I don't believe it is. Throughout our program we do something like this:
C_TEXT(<>CR)
C_TEXT($var)
<>CR:=Char(13) //carriage return
$var:="First Line" + <>CR + "Second Line" + <>CR + "Third Line"
We also have a linefeed: <>LF:=Char(10). We use IP variables that are defined during program startup for these characters and a few others, Tab comes to mind. You could use constants, but we've had some issues with those in the past so we try not to use them when possible.
You can also use a \n or \r within the text. In the debugger it will just show them all on one line with the \n and \r, but I believe it will display multiline (it does in an Alert()).
$var:="First Line\nSecond Line\nThird Line"
We choose the first way for readability and maintainability.

New lines after grep a binary file are missing

Im trying to get text from text layers in a PSD file, under linux.
Now Im using:
egrep -a 'LayerText' file.psd
<photoshop:LayerText>免费获得宝贵资源! \ 工业现场过程仪表校准测试和维护诊断的必备工具 福禄克过程校准器,为工作在过程行业的技术工程师,自动化系统维护和仪表工程师,质量控制工程师,计量人员提供全面的工业校准测试和维护诊断工具:包括智能认证校准器,多功能信号校准器,压力校准器,温度校准器,环路校准器以及其他过程信号故障诊断和检测工具。FLUKE过程校准及检测工具,在化工、电力、石油、纸浆、食品饮料、制造业和污水处 理/给排水等行业的现场校准及检测维护方面处于世界领先水平。过程校准的全系列产品,从简单的回路校准器到复杂的文档化全功能过程校准器,可以提供各种必需的温度、压力、电流、电压以及电阻和频率的校准。来自福禄克750系列的校准管理软件,更是满足了用户 日益增长的对现场仪表校准数据进行归档整理的需求</photoshop:LayerText>
But using Photoshop the text layer has new lines after:
免费获得宝贵资源!
工业现场过程仪表校准测试和维护诊断的必备工具
How can I parse and output the text separated by real newlines and not all in one single line.
Thanks in advance.
It looks like your file does not contain a regular newline character but something else (looks like two spaces around a backslash).
If you want to separate the files using this (which looks like unusual) line separator, you can do that e.g. using sed(1).

preserving linebreaks in values in JSON key-value pairs

I am not sure if line breaks are allowed in JSON values. I certainly am unable to create the following in JSON
{"foo": "I am not sure if line breaks are
allowed in JSON values. I certainly
am unable to create the following in JSON"}
The following certainly does not work
{"foo": "I am not sure if line breaks are\nallowed in JSON values. I certainly\nam unable to create the following in JSON"}
Preamble: I want to send a long message like above either to the browser or to the console app and display it neatly formatted so it is legible to the user.
If you are displaying (or inserting) the json value directly in HTML, you can't use it as it is because in html new lines are ignored and replaced by a space.
For example if you have:
<p>Hello,
I'm in other line.</p>
It will be represented as:
Hello, I'm in other line.
You must convert the new lines to paragraph or <br>, for example:
<p>Hello,<br>
I'm in other line.</p>
That will be show as:
Hello,
I'm in other line
If this is your case, you can simply use String.replace to change \n into <br>\n.
If you're displaying it in HTML, will simple do.
Or in a text area or something, try "\r\n", wrapped in double quotes.
Double backslashes are to escape.