简体   繁体   中英

parse array of json with node.js

I'm new to node.js so apologies if this is something very simple.

I have the below node js script:

 var http = require("http"); var bodyParser = require("body-parser"); var options = { "method": "GET", "hostname": "xxx.xxx.xxx.xxx", "port": "18080", "path": "/api/v1/applications/app-20180103124606-0007/stages/0" }; var req = http.request(options, function (res) { var chunks = []; res.on("data", function (chunk) { chunks.push(chunk); }); res.on("end", function () { var body = JSON.parse(Buffer.concat(chunks)); console.log(body); }); }); req.end();

It returns this JSON:

 [ { status: 'COMPLETE', stageId: 0, attemptId: 0, numActiveTasks: 0, numCompleteTasks: 1, numFailedTasks: 0, executorRunTime: 2738, executorCpuTime: 1207164005, submissionTime: '2018-01-03T12:46:10.796GMT', firstTaskLaunchedTime: '2018-01-03T12:46:10.810GMT', completionTime: '2018-01-03T12:46:14.513GMT', inputBytes: 0, inputRecords: 99171, outputBytes: 0, outputRecords: 0, shuffleReadBytes: 0, shuffleReadRecords: 0, shuffleWriteBytes: 1468516, shuffleWriteRecords: 3872, memoryBytesSpilled: 0, diskBytesSpilled: 0, name: 'reduceByKey at /scripts/wordcount.py:37', details: 'org.apache.spark.rdd.RDD.<init>(RDD.scala:104)\norg.apache.spark.api.python.PairwiseRDD.<init>(PythonRDD.scala:391)\nsun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)\nsun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)\nsun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)\njava.lang.reflect.Constructor.newInstance(Constructor.java:423)\npy4j.reflection.MethodInvoker.invoke(MethodInvoker.java:247)\npy4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)\npy4j.Gateway.invoke(Gateway.java:236)\npy4j.commands.ConstructorCommand.invokeConstructor(ConstructorCommand.java:80)\npy4j.commands.ConstructorCommand.execute(ConstructorCommand.java:69)\npy4j.GatewayConnection.run(GatewayConnection.java:214)\njava.lang.Thread.run(Thread.java:748)', schedulingPool: 'default', accumulatorUpdates: [ [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object] ], tasks: { '0': [Object] }, executorSummary: { '0': [Object] } } ]

I now need to extract launchTime and taskTime. How is this done? I had previousy managed to extract data from JSON but I think I'm having trouble here as it's contained in an array.

You can use Array.prototype.forEach() to iterate over each member of the response body.

 res.on('end', () => { let body = JSON.parse(Buffer.concat(chunks)) body.forEach(item => { // Do something with item console.log(item) }) })

If the array length is always 1, then you can access the first array element with body[0] .

 body[0].executorRunTime body[0].firstTaskLaunchedTime

Otherwise you could iterate over the result with body.forEach() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM