DEV Community

Judy
Judy

Posted on

2 1 1 1 1

Convert CSV Data into Multilevel JSON #eg56

Problem description & analysis

Below is data in CSV file csv.csv:

metric,value,date

temp_a,622.0,1477895624866

temp_a,-3.0,1477916224866

temp_a,365.0,1477917224866

temp_b,861.0,1477895624866

temp_b,767.0,1477917224866

We are trying to convert the categorized CSV data into multilevel data, as shown below:

[
  {
    "metric":"temp_a",
    "datapoints":[
      [622, 1477895624866],
      [-3, 1477916224866],
      [365, 1477917224866]
    ]
  },
  {
    "metric":"temp_b",
    "datapoints":[
      [861, 1477895624866],
      [767, 1477917224866]
    ]
  }
]
Solution

Write the following script p1.dfx in esProc:

Image description
Explanation:

A1   Import the CSV data as a table sequence.

A2  Group A1’s data by metric field. Each group contains several pairs of [value,date], which are named datapoints.

A3  Convert the multilevel data into JSON format.

SPL open source address

Download

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay