finferflu
finferflu

Reputation: 1378

Extracting JSON generic string from text file

I have a file the contents of which are formatted as follows:

{
  "title": "This is a test }",
  "date": "2017-11-16T20:47:16+00:00"
}

This is a test }

I'd like to extract just the JSON heading via regex (which I think is the most sensible approach here). However, in the future the JSON might change, (e.g. it might have extra fields added, or the current fields could change), so I'd like to keep the regex flexible. I have tried with the solution suggested here, however, that seems a bit too simplistic for my use case: in fact, the above "tricks" the regex, as shown in this regex101.com example.

Since my knowledge of regex is not that advanced, I'd like to know whether there's a regex approach that is able to cover my use case.

Thank you!

Upvotes: 0

Views: 2092

Answers (2)

Slai
Slai

Reputation: 22876

You can check for the first index of \n} to get the sub-string:

s = `{
  "title": "This is a test }",
  "date": "2017-11-16T20:47:16+00:00"
}
This is a test }
}`

i = s.indexOf('\n}')

if (i > 0) {
  o = JSON.parse(s = s.slice(0, i + 2))
  console.log(s); console.log(o)
}

or a bit shorter with RegEx:

s = `{
  "title": "This is a test }",
  "date": "2017-11-16T20:47:16+00:00"
}
This is a test }
}`

s.replace(/.*?\n}/s, function(m) {
  o = JSON.parse(m)
  console.log(m); console.log(o)
})

Upvotes: 1

Barmar
Barmar

Reputation: 781964

If the JSON always starts with { at the left margin and ends with } at the right margin, with everything else indented as you show, you can use the regular expression

/^{.*?^}$/ms

The m modifier makes ^ and $ match the beginning and end of lines, not the whole string. The s modifier allows . to match newlines.

var str = `{
  "title": "This is a test }",
  "date": "2017-11-16T20:47:16+00:00"
}

This is a test }
`;

var match = str.match(/^{.*?^}$/ms);
if (match) {
  var data = JSON.parse(match[0]);
}
console.log(data);

Upvotes: 1

Related Questions