5

I have a text file data.txt:

Framework1,Version1
Framework2,Version2
Framework3,Version3

I need to convert it to data.json which would look like:

[
{"FrameworkName":"Framework1", "VersionName":"Version1"},
{"FrameworkName":"Framework3", "VersionName":"Version2"},
{"FrameworkName":"Framework3", "VersionName":"Version3"}
]

I have tried using awk but it hasn't helped me much. Any help would be appreciated.

1
  • What is the data type in txt file? I mean input data type. Commented Mar 2, 2018 at 11:27

1 Answer 1

6

jq solution:

jq -Rs '[ split("\n")[] | select(length > 0) 
          | split(",") | {FrameworkName: .[0], VersionName: .[1]} ]' data.txt

The output:

[
  {
    "FrameworkName": "Framework1",
    "VersionName": "Version1"
  },
  {
    "FrameworkName": "Framework2",
    "VersionName": "Version2"
  },
  {
    "FrameworkName": "Framework3",
    "VersionName": "Version3"
  }
]

https://stedolan.github.io/jq/manual/v1.5/

Sign up to request clarification or add additional context in comments.

1 Comment

Lovely... Also output all this to a json file [your above code] > file.json :)

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.