javapatriot
javapatriot

Reputation: 353

Parse JSON string using node.js

I'm looking for some regex help on the following:

{ Start: '2019-10-21T15:00:00Z', End: '2019-10-21T15:30:00Z' }

I need to be able to just pull the start value from what is above. Ex: 2019-10-21T15:00:00Z

My regex is terrible and I don't even really have any semi-functional code to share.

Any help would be appreciated.

Upvotes: 0

Views: 212

Answers (2)

Malcor
Malcor

Reputation: 2721

Couple of things

this is not valid JSON.

{ Start: '2019-10-21T15:00:00Z', End: '2019-10-21T15:30:00Z' }

This is valid JSON.

'{ "Start": "2019-10-21T15:00:00Z", "End": "2019-10-21T15:30:00Z" }'

You can parse this using JSON.parse(), which will parse your string into a javascript object.

var jsonString = '{ "Start": "2019-10-21T15:00:00Z", "End": "2019-10-21T15:30:00Z" }';

var parsedJson = JSON.parse(jsonString);
console.log(parsedJson.Start);
console.log(parsedJson.End);

Upvotes: 1

bitinerant
bitinerant

Reputation: 1551

You didn't specify what tool you are using. Regex works slightly differently in Python, Perl, JavaScript, grep, etc.

For Perl, you need: Start: .\K[0-9TZ:-]+

To test this on the command-line:

echo "{ Start: '2019-10-21T15:00:00Z', End: '2019-10-21T15:30:00Z' }" |grep -Po 'Start: .\K[0-9TZ:-]+'

Upvotes: 2

Related Questions