Hi, I'm a beginner in Perl.
I would like to use the JSON module in order to create one. In other words, I'm trying to build a function which creates a JSON file:

Consider the following structure of a JSON file:

{ "time": 123456 "id": 56789 "data": [{"key": "first" , "value": "1" , "format": "1.5.6" , "ver +sion": "5.4"}, {"key": "two" , "value": "2" , "format": "1.4.6" , "versi +on": "5.4}, {"key": "five" , "value": "5" , "format": "1.5.9" , "vers +ion": "5.1"}] }
This is the structure I would like the JSON file to be. Also, consider that the data is in a file that each one if its lines look like this:
first,1,1.5.6,5.4 two,2,1.4.6,5.4 five,5,1.5.9,5.1

So the array of objection that JSON file should contain is represented as each line of the previous file. I know how to split each line and push into a hash. So I would like to ask the two following questions:

1. Is there a better way to than splitting and pushing into a hash?
2. Main question: After I get a hash, how can I convert it to be of JSON file type while not forgetting to add the primitive values of "time" & "id"?
I saw some examples of how to convert JSON to hash in Perl but didn't find an example of how to use the other way.

Thank you and have a great day.

In reply to Create JSON file in specific format by ovedpo15

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.