0

I want to convert a text file to JSON, but in a very particular way. My text file looks like this:

Data 1:
datapoint1-1 = 21
datapoint1-2 = 23
Data 2:
datapoint2-1 = 21
datapoint2-2 = 23
datapoint2-3 = 23

I want to create a JSON file that separates this data like this:

{
 {
 "Data": "1",
 "Datapoints": [
   {
    "datapoint1-1": "21",
    "datapoint1-2": "23"
   }
  ]
 },
 {
 "Data": "2",
 "Datapoints": [
  {
   "datapoint2-1": "21",
   "datapoint2-2": "23",
   "datapoint2-3": "23"
  }
 ]
 }
}

My first step has split the data into 2 arrays inside an array. The first array is Data 1 plus its data-points and the second is Data 2 plus its data-points.

Now I am stuck on how I can convert those arrays into the JSON format I want. Does anyone have an idea? or can direct me in the right direction for this?

Thanks,

6
  • 7
    Did you try anything? Commented Dec 31, 2018 at 3:41
  • I haven't found anything helpful. Only thing I've tried is a simple JSON.stringify(obj) but it isn't specific enough to what I want as an outcome. Commented Dec 31, 2018 at 3:46
  • JSON.stringify tend to convert text format (those are like json format) to proper json format. Commented Dec 31, 2018 at 3:49
  • Your desired output isn't valid javascript or JSON. Is that supposed to be an array or an object? And why is Datapoints an array if it's always just going to have one item? Commented Dec 31, 2018 at 3:51
  • I made it into an array because I thought it'd be easier to use in my code, in case I want to reference a specific data point. Commented Dec 31, 2018 at 4:09

2 Answers 2

5

Here is my solution

const input = `
  Data 1:
  datapoint1-1 = 21
  datapoint1-2 = 23
  Data 2:
  datapoint2-1 = 21
  datapoint2-2 = 23
  datapoint2-3 = 23
`

const array = input.split('\n').reverse()
const response = []
let template = {}
template['Datapoints'] = []
let switcher = false

array.map(arr => {
  // remove empty strings
  if (arr) {
    if (arr.includes('datapoint')) {
      const keyValue = arr.split(' = ')
      template.Datapoints.push({ [`${keyValue[0]}`]: keyValue[1] })
    } else {
      const keyValue = arr.split(' ')
      template.Datapoints.reverse()

      template[keyValue[0]] = keyValue[1].slice(0, -1)
      switcher = true
    }

    if (switcher) {
      response.push(template)
      template = {}
      template['Datapoints'] = []
      switcher = false
    }
  }
})

const finalResponse = response.reverse()

console.log(JSON.stringify(finalResponse, null, 2))

and in console you got

[
  {
    "Datapoints": [
      {
        "datapoint1-1": "21"
      },
      {
        "datapoint1-2": "23"
      }
    ],
    "Data": "1"
  },
  {
    "Datapoints": [
      {
        "datapoint2-1": "21"
      },
      {
        "datapoint2-2": "23"
      },
      {
        "datapoint2-3": "23"
      }
    ],
    "Data": "2"
  }
]
Sign up to request clarification or add additional context in comments.

Comments

0

To convert text files in JSON you can use the JACKSON OBJECT MAPPER jar in your code. Create a simple POJO. It will read JSON string from a file and map it to your class. Then you would need to convert the JSON string value to java object. This will help you with the same.

1 Comment

Hi there, the question specifically asks for NodeJs solution (not a java one).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.