Kelly Norton
Kelly Norton

Reputation: 497

node.js convert string containing whitespace and nested strings to json

I'm working on a Lambda function that will push AWS ELB logs in to ElasticSearch. I'm reading the ELB logs from S3, and each file contains around 500 lines such as this:

2016-08-10T01:03:42.216242Z foaas 66.249.79.176:61945 172.31.45.185:80 0.000044 0.194952 0.000039 200 200 0 30 "GET http://www.foaas.com:80/you/JSON/kelly HTTP/1.1" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" - -

Since the string contains whitespace, semicolons, nested strings, plus signs etc. I'm really at a loss on the best way to convert each line to JSON. I've tried playing with split() but can't get it to play nice given all the unusual characters in the string.

My plan is to setup key:value pairs using the fields documented by AWS here.

Any suggestions would be most welcome.

Thank you.

Upvotes: 1

Views: 686

Answers (1)

deeveeABC
deeveeABC

Reputation: 986

You can use the JSON function:

JSON.parse(yourStringYouWantToPass);

To remove white spaces you can use regex like this:

.replace(/\s+/g, '');

The \s is regex for 'whitespace', and g is the "global" flag, meaning ALL \s (white spaces).

Upvotes: 1

Related Questions