-4

I have following string

{
  "browser": "firefox",
  "dateTime": "28_May_2014_03_35_PM"
}
{
  "browser": "firefox",
  "dateTime": "28_May_2014_03_36_PM"
}

I want to get elements betwen opening { and closing } braces.

16
  • See here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented May 30, 2014 at 6:14
  • 3
    You must live under a rock or something. This is JSON and you can parse it into an object using JSON.parse(str) Commented May 30, 2014 at 6:14
  • 2
    I'm wondering why an 800 rep user is asking a question like this Commented May 30, 2014 at 6:17
  • 2
    If always look like this, you can add an extra ','. Whenever you find a single '}' Commented May 30, 2014 at 6:27
  • 2
    Do you have control over how the "JSON" is generated in the first place? If so, I would suggest that you fix that. It's misleading to have a URI that suggests it returns a JSON-formatted output when it doesn't. Commented May 30, 2014 at 7:17

2 Answers 2

1

It looks quite like a JSON encoded objects, but i see there are some incongruences, Please pay attention at the little edits in you starting string:

JSON.parse('[{"browser": "firefox","dateTime": "28_May_2014_03_35_PM"},{"browser": "firefox","dateTime": "28_May_2014_03_36_PM"}]');

//this will convert your string to an actual javascript object..

EDIT:

If your json in not proper encoded, you'll have to fix here and there offcourse.. you must add

  • enclosing square brackets: [{ ... }]
  • a comma after every object , {}, {}

You can accomplish that with this code

assume that var_string will contain your string..

var_string = var_string.replace(/(\r\n|\n|\r)/gm,""); //removing line breaks;

var_string += '[' + var_string + ']'; //adding square brackets

var_string = var_string.replace(/}{/g, "},{"); //adding commas

var_string = JSON.parse(var_string); //parsing object

Json Syntax

Sign up to request clarification or add additional context in comments.

6 Comments

This is not answering the question at all
i'm editing since the question has been cleared up in a second moment
@rpax Why we are so nervous :).. this is a fantastic community! let's help each other!
what happens if the json contains { inside ? . "key": "{", for example?
{ "browser": {"x":"hello"}, "dateTime": "28_May_2014_03_35_PM" } it's still going to work, but theese are assumption because we don't actually know everything about the input string. @rpax
|
1

Sanjay, with all the disclaimers already offered about using regex, since you did ask what regex could do for you, here is an expression that would match the two tokens in each of your elements. It assumes that each element only has two tokens, so let us know if that is not the case.

{\s*"([^"]*)":\s*"([^"]*)",\s*"([^"]*)":\s*"([^"]*)"
  1. The first token's name is captured to Group 1, its value is captured to Group 2.
  2. The second token's name is captured to Group 3, its value is captured to Group 4.

In the demo, look a the Groups in the lower right panel.

What the regex means

The top right panel of the demo has a token-by-token explanation.

How to use the regex

This is probably obvious for you, but here is sample code to retrieve the values (see output in code demo):

<script>
var subject = '{ \
  "browser": "firefox", \
  "dateTime": "28_May_2014_03_35_PM" \
} \
{ \
  "browser": "firefox", \
  "dateTime": "28_May_2014_03_36_PM" \
} \
';
var regex = /{\s*"([^"]*)":\s*"([^"]*)",\s*"([^"]*)":\s*"([^"]*)"/g;
var match = regex.exec(subject);
while (match != null) {
    document.write("First token name: ",match[1],"<br />");
    document.write("First token value: ",match[2],"<br />");
    document.write("Second token name: ",match[3],"<br />");
    document.write("Second token name: ",match[4],"<br />");
    match = regex.exec(subject);
}

</script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.