0

The text that I have is

 "Example": "Example2",
 "What": "Huh",
 "Really": "yes",

The text inside the "" always changes, but everything else always stays the same. I need to put that into this format:

"Example2": {
        "Name": "Example",
    },
"Huh": {
        "Name": "What",
    },
"yes": {
        "Name": "Really",
    },

Here it's the same. The text inside the "" always changes but the rest stays the same. As you might have noticed, the text also gets inverted, meaning that the "Example" that was in the front is now in the back and vise versa. Do you have an idea on how to automate this process?

My OS: macOS, Kali Linux, and Win11

Notes: The quotes indicate string, as this will be used for programming they are just used to tell the computer that that is a string, they could either be transferred from the original form to the second form, or they could be in the second form the whole time, it really doesn't matter. Also, only letters, ( ), - , _ , dot, and comma can appear inside the text that's inside the "".

What have I tried so far: When I tried to google it, I found only formatting (like italics, bold etc.) not the thing that I want, meaning that I don't even know where to begin, don't know the tool I need/want to use, and haven't tried any tool except zapier formater

2
  • Welcome to Super User. Please respond by editing the question. (1) In what OS are you willing to automate this? (2) Quotes are mandatory in the input and in the output. Please confirm. (3) Can " appear inside quotes? How can it be escaped then? (4) Reasonable research effort is advised. What have you tried so far? Where are you stuck? Commented May 27, 2022 at 22:49
  • You want to make a Json file, which fairly simple. Besides pondering, What have you tried so far? What tools are having problems with?
    – dmb
    Commented May 27, 2022 at 23:00

2 Answers 2

0

The following sed command will identify the first two double-quoted strings per input line and output them in the desired format:

<input_file sed 's#[^"]*"\([^"]*\)"[^"]*"\([^"]*\)".*#"\2": {\n\t\t"Name": "\1",\n\t},#'

Notes (useful if you want to modify the solution for whatever reason):

  • I used tabs (\t) for indentation. Replace with spaces if you want.

  • * is greedy, but [^"] means "any character but "". In effect the code captures the first two quoted strings. The rest of the input syntax is irrelevant. By using .* instead of [^"]* you can capture the last two quoted strings:

    <input_file sed 's#.*"\(.*\)".*"\(.*\)".*#"\2": {\n\t\t"Name": "\1",\n\t},#'
    

    This works equally well for the example in question, but conceptually it's different. Despite this version being simpler as sed code, I prefer my original solution. The reason is: if the input format is ever extended and allows an additional (possibly optional) double-quoted string(s), it's likely the extra field(s) will appear after the already defined ones. The code with [^"]* will be compatible with the extended format (within the scope of the two fields it is designed to handle). The code with .* would be better if the format was extended by adding fields in front, this seems less likely.

  • I could include the quotes in the capture groups, this would result in slightly shorter replacement code (e.g. \2: instead of "\2":). I decided not to do this, because I find the quotes belong to the syntax, not to the data. My regex abstracts input data without anything related to the syntax. There's some elegance in this approach. Now I can build any output format by adjusting just the replacement part of s (not touching the regex).

0

With that information, use a bash terminal and use awk

awk '{print $3, "\:"}{print "\{name\:", $1,"\},"}' file.txt > file.json

sample testjason.txt

"example" : "answer",
"what" : "huh",
"really" "yes",

output

enter image description here

This works because awk processes the lines in the file.txt as arrays so you can access them with $n. Curly braces {} create a context where you use built in command in this case print and besides using variables you use strings, some needs to be scaped using \ like \" as they are operators otherwise.

Finally the > bash operator is to redirect the output to a new file in the current working directory

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .