Flatten Json
View all Actors
This Actor is unavailable because the developer has decided to deprecate it. Would you like to try a similar Actor instead?
See alternative ActorsFlatten Json
petr_cermak/flatten-json
This act extracts all arrays from input JSON and extends their elements with the core object.
Dockerfile
1# First, specify the base Docker image. You can read more about
2# the available images at https://sdk.apify.com/docs/guides/docker-images
3# You can also use any other image from Docker Hub.
4FROM apify/actor-node:16
5
6# Second, copy just package.json and package-lock.json since those are the only
7# files that affect "npm install" in the next step, to speed up the build.
8COPY package*.json ./
9
10# Install NPM packages, skip optional and development dependencies to
11# keep the image small. Avoid logging too much and print the dependency
12# tree for debugging
13RUN npm --quiet set progress=false \
14 && npm install --only=prod --no-optional \
15 && echo "Installed NPM packages:" \
16 && (npm list || true) \
17 && echo "Node.js version:" \
18 && node --version \
19 && echo "NPM version:" \
20 && npm --version
21
22# Next, copy the remaining files and directories with the source code.
23# Since we do this after NPM install, quick build will be really fast
24# for most source file changes.
25COPY . ./
26
27# Optionally, specify how to launch the source code of your actor.
28# By default, Apify's base Docker images define the CMD instruction
29# that runs the Node.js source code using the command specified
30# in the "scripts.start" section of the package.json file.
31# In short, the instruction looks something like this:
32#
33# CMD npm start
main.js
1const Apify = require('apify');
2
3function extractArrays(obj){
4 const arrays = {};
5 for(let key in obj){
6 const val = obj[key];
7 if(Array.isArray(val)){
8 arrays[key] = val;
9 delete obj[key];
10 }
11 }
12 return arrays;
13}
14
15function extendArray(array, name, extObj){
16 const result = [];
17 for(let elem of array){
18 result.push(({[name]: elem,... extObj }));
19 }
20 return result;
21}
22
23function getExtended(obj){
24 let results = [];
25 const arrays = extractArrays(obj);
26 for(let key in arrays){
27 results = results.concat(extendArray(arrays[key], key, obj));
28 }
29 return results;
30}
31
32Apify.main(async () => {
33 const input = await Apify.getValue('INPUT');
34
35 let output = [];
36 for(let inObj of input){
37 output = output.concat(getExtended(inObj));
38 }
39
40 await Apify.setValue('OUTPUT', output);
41});
package.json
1{
2 "name": "my-actor",
3 "version": "0.0.1",
4 "dependencies": {
5 "apify": "^2.2.2"
6 },
7 "scripts": {
8 "start": "node main.js"
9 },
10 "author": "Me!"
11}
Developer
Maintained by Community
Categories