0

I have some code that returns something like this:

body: '[
    {
        name: "name",
        lastname: "lastname"
    },
    {
        name: "name",
        lastname: "lastname"
    },
    {
        name: "name",
        lastname: "lastname"
    }
]'

Of course extracting the body is a simple object.body however, to get rid of the '' that wraps the array is just destroying me. I tried doing object.body.slice(1,-1) to get rid of them, it didnt work. I'm clearly not using the object properly.

How can I "extract" the array from within the body into a usable array?

1
  • Where is that data coming from? Can you control its form? If you would get valid JSON, you could use JSON.parse Commented Aug 20, 2017 at 19:35

3 Answers 3

2

It sounds like you want to evaluate the content of the string. Assuming whatever code building this string can't actually build JS objects to begin with, you can use eval or Function to generate the objects you need.

var data = eval(string);

Note that you must be sure that the source of the string is safe and reliable, otherwise you could be evaluating malicious code. There may also be performance consequences to using eval.

Using Function is a tiny bit safer, only because the code can not access your local variables. It should also avoid the performance costs of eval.

var data = Function("return (" + string + ");")();

Same warning about malicious code though.


If the string data was valid JSON, you could use JSON.parse, but it isn't, so you can't. To avoid security issues, either have the source provide valid JSON data, or write your own minimal parser to parse the data.

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

8 Comments

There must be another way. eval is never a good idea, not even if you're 100% sure (because you can't be)
@Aenadon: eval/Function is a very good idea in those circumstances where you can guarantee safety. Don't play the "never" game. Instead be informed. My answer gives the necessary warnings.
@Aenadon in this situation this is a good idea. Eval will interpret the string to generate a array based on the string.
No! eval should never be used without sanitizing the given parameter!
@lumio: eval is a useful tool, as long as you know when to use it. Knowledgeable programmers don't need to resort to these knee-jerk reactions. You do yourself and others a disservice with such extreme overreactions.
|
0

With a valid JSON string, you could just parse the string for an object with JSON.parse, if you have .

array = JSON.parse(string);

4 Comments

You can't do that, since the keys are not quoted.
You Ninja'd me :-(
Parse actually worked. I apologize for the RIDICULOUSLY STUPID question. been coding for 9 hrs straight today and my brain just can't process anymore. I'll accept the answer asap and upvote all of you friends :)
@GalAppelbaum Either the string you've posted in your question doesn't reflect your actual data, or JSON.parse won't work. The string in the question is not valid JSON, since the keys of the objects are not enclosed in double quotes.
0

You can use split function and then take the first element, let me say

var a = '[{name: "name",lastname: "lastname"},{name: "name",lastname: "lastname"},{name: "name",lastname: "lastname"}]';
    var s = a.split("'");

s[0] will return you the desired result

2 Comments

The variable a does not contain single quote '. What are you splitting then?
sorry, it's my mistake! What i mean is that suppose var a is a string containing those single quotes as your situation is! so in this case var a = '\'[{name: "name",lastname: "lastname"},{name: "name",lastname: "lastname"},{name: "name",lastname: "lastname"}]\''; var s = a.split("'"); s[1] will be the wanted result!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.