Skip to content
Advertisement

Convert nested array of array to nested array of object

I have currently the challenge to convert the following data:

JavaScript

into the following:

JavaScript

I’m currently a bit lost how I could convert the source data into the expected object.

I get the data from a sql query ( presto ).

The structured information ( fieldname2 and fieldname3 ) aren’t resolved, I get only the values in the response.

The field position at root level is dynamic ( could be fieldname1, fieldname2, fieldname3 but also fieldname3, fieldname1, fieldname2).

The value position inside fieldname2 and fieldname3 seems to be static.


I already solved the challenge to convert the given table schema into a JSONSchema:

JavaScript

Based on the JSONSchema I was able to generate a “model definition” which will be used in a code generator:

JavaScript

Advertisement

Answer

Based on the comment from @nikhil, I spent an hour to create a running prototype.

I have tested it successfully with the provided example data and real data.

It worked in both cases.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement