Need help for given input to convert in json

Input is in text file-
Topics to explore
Birds
Species
Description Black
Animals
Species
Description Black

Output should be in json format for the given input.

Note:- only one thing should be displayed either birds or animal depending on the input text file.

Can you give two examples of your input texts CSV file ?
If I understand depending of the given input : the output must be a Json file ?

Have a look to this sample : Go Playground - The Go Programming Language

Sample input 1-
Topics to explore
Birds
Species
Description Black

Sample input 2-
Topics to explore
Animals
Species
Description Black

Output should be in json format for the given input with using package name kingdom and not package main and func main

What are the rules for the input files? How is the data format specified?

How does it need to look like in JSON?

Just wrapping your input files text in quotes and replacing newlines with literal \n would make it valid JSON. Though that probably isn’t what you need.

Also, if you show some code that shows your current state, and explain what it does instead of what it should do, increase your chance for answers.

Sample Output 1-
Topics to explore
{
“Birds:”
{
“Species”,
“Description:” “Black”
}
}
Sample Output 2-
Topics to explore
{
“Animals:”
{
“Species:”,
“Description:” “Black”
}
}

I hope this helps

Nope, it doesn’t, as that JSON is not syntactically valid.

Also, whenever you want to show some code snippets, please use markdown fenced code blocks or intend by additional 4 spaces.

```json
{"foo": 1}
```

    {"foo": 1}

Will be rendered as:

{"foo": 1}
{"foo": 1}

Sample json output 1-
{
“Topics to explore”: [
{
“Birds”: [
“Species”,
“Description Black”
]
}
]
}

Sample json output 2-
{
“Topics to explore”: [
{
“Animals”: [
“Species”,
“Description Black”
]
}
]
}

You are using lists here, how would an input look like that causes more than a single entry?

What is the specification of the input format?

What is the code you already have?

How does it behave differently from what you expect?

Please help us to help you!

Hi i dont have the code i was asked to use map and slices. I really need way to this . Please kindly help with code. I don’t have any other information left to give.

Then write code. Once it fails, we can help you fix it. We won’t do your homework.

Hi, @Gau_ty,

Firstly: It is my understanding that you have several text files such as:

Sample input 1.txt

Topics to explore
Birds
Species
Description Black

Sample input 2.txt

Topics to explore
Animals
Species
Description Black

And you want to transform each of these text files into separate JSON files such as:

Sample json output 1.json

{
  "Topics to explore": [
    {
      "Birds": [
        "Species",
        "Description Black"
      ]
    }
  ]
}

Sample json output 2.json

{
  "Topics to explore": [
    {
      "Animals": [
        "Species",
        "Description Black"
      ]
    }
  ]
}

Is that correct?

Secondly: It is unlikely that anyone on this site will write an entire program that solves your whole problem and give it to you. There are other web sites where you can pay people to do that for you, but the goal of the members on this site is to help guide you so you can solve the problem (mostly) yourself.

With that goal in mind, at this time, my suggestions to you are:

  1. Please confirm the format of the input (separate .txt files?) and output (separate .json files?). Knowing exactly what you start with and what you want to end with will help us guide you better.

  2. Write down the steps that the code will have to execute to do what you want. You do not have to write them down in Go; you can use “pseudo-code” or English, or whatever other language you are comfortable with.

  3. Try writing the code for each step. If you get stuck, it is at this point that the members of this forum will be able to help you more directly, and possibly with code. For example:

    New question: “I have multiple .txt files like ‘sample input 1.txt,’ ‘sample input 2.txt,’ etc., and I want to open each one in a loop, but I don’t know how many there could be. Maybe up to ‘sample input 10.txt’ or ‘sample input 100000.txt.’ How do I find all the files? Here’s my code so far: …”

    For a question like this, you could potentially receive several answers from several members on this forum, some of which may contain code. This question does not ask anyone to write the code for you; it is a request for guidance after you have thought about the problem and you are stuck.

3 Likes

Yes your input and output is right. Input is in 1 .txt format file and output is is in one .json format.
I will try to code it but here pseudo how it should go around

Package Kingdom
import( necessary import files.
)
Const comand =“Topics to explore”
Type output struct{
Unpaarsedlines []string json:"unpaarsedlines,omiempty"
Mammals map[string][] json:"mammals"
}
This mammals should have birds and animals.

The thing with input is that it can increase its not fixed. Like
Sample input 1-
Topics to explore
Birds
Blaha blaha

Sample input 2-
Topics to to explore
Birds
Species
Description Black
Blaha. Blaha

Sample input 3-
Topics to explore
Animals
Blah blah
Description Black
Blah blah

  1. In my question, I asked if the input and output were separate files, but your answer seems to imply that the input and output are each a single file. Is there one input file and one output file?

  2. If so, it looks to me like the separator between each record in the input file is “Sample input n-” where n is an incrementing number. Is that true?

  3. Can you elaborate on the relationship between

    Type output struct{
    Unpaarsedlines []string json:"unpaarsedlines,omiempty"
    Mammals map[string][] json:"mammals"
    }
    

    and your sample JSON:

    {
      "Topics to explore": [
        {
          "Animals": [
            "Species",
            "Description Black"
          ]
        }
      ]
    }
    

    It looks like the { "Animals": [ ... ] } part might correspond to the Mammals field in the output struct, but the type definition isn’t complete: You have map[string][] ← but you need a type name here.

Hi
Yes there is only one single input file and one single output file named as inpu.txt and output.json respectively.

Coming to later part the birds or animals aren’t fixed it can change to anything like humans too. So the basic idea was to use mammals map[string][]string and store it as dictionary with slices.

Something like this
Mammals : {
“Human” :{
Species,
Description Black,
Blaha blaha
},
And so on
}

This is again not valid JSON, not even something one could read as valid JSON with a bit of squinting…

And you still are not showing what you have tried so far or where exactly your problem is.

Are you struggling with opening and reading the input file?

Is it writing the output file?

Transform the input to your internal representation?

Transform the internal representation to JSON?

So it sounds like if your input.txt file looks exactly like this:

Sample input 1-
Topics to explore
Birds
Blaha blaha

Sample input 2-
Topics to to explore
Birds
Species
Description Black
Blaha. Blaha

Sample input 3-
Topics to explore
Animals
Blah blah
Description Black
Blah blah

Then you want output.json to look exactly like this:

{
  "Topics to explore": [
    {
      "Birds": [
        "Blaha blaha",
        "Species",
        "Description Black",
        "Blaha. Blaha"
      ],
      "Animals": [
        "Blah blah",
        "Description Black",
        "Blah blah"
      ]
    }
  ]
}

I’ve assumed that if you have two Sample input n- sections with the same “key” (“Birds”, “Animals”, etc.) under “Topics to explore,” then the subsequent values (“Species”, “Description Black”, etc.) should just be appended to. For example, both your “Sample input 1-” and “Sample input 2-” sections say “Birds,” so I assumed 1’s value, “Blaha blaha” should be first and 2’s values: “Species”, “Description Black”, and “Blaha. Blaha” should be appended after.

Is this correct?

Yes correct

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.