254

I have a string as

string = "firstName:name1, lastName:last1"; 

now I need one object obj such that

obj = {firstName:name1, lastName:last1}

How can I do this in JS?

rahul
  • 184,426
  • 49
  • 232
  • 263

20 Answers20

214

Actually, the best solution is using JSON:

Documentation

JSON.parse(text[, reviver]);

Examples:

1)

var myobj = JSON.parse('{ "hello":"world" }');
alert(myobj.hello); // 'world'

2)

var myobj = JSON.parse(JSON.stringify({
    hello: "world"
});
alert(myobj.hello); // 'world'

3) Passing a function to JSON

var obj = {
    hello: "World",
    sayHello: (function() {
        console.log("I say Hello!");
    }).toString()
};
var myobj = JSON.parse(JSON.stringify(obj));
myobj.sayHello = new Function("return ("+myobj.sayHello+")")();
myobj.sayHello();
Matej
  • 9,548
  • 8
  • 49
  • 66
  • won't allow functions to be passed in though – K2xL Jan 04 '14 at 18:17
  • 2
    That is true, however strictly speaking functions should not be in any JSON objects.. For that you have RPC etc, or if you want, you can pass the prototype of a function to the json, and do `eval` later. – Matej Jan 04 '14 at 21:39
  • 65
    Doesn't answer the question at all I'm afraid. It would be wonderful if everyone changed the question to suit the answer. How do you parse "firstName:name1, lastName:last1"? not '{ "hello":"world" }'. – Zephyr was a Friend of Mine Sep 12 '14 at 14:31
  • @NoelAbrahams good observation! Just noticed that, I assumed the string is in JSON format.. – Matej Sep 12 '14 at 15:07
  • 51
    This doesn't answer the question, I don't know why it has so many upvotes. The questioner (and myself, I googled and ended up here) have data that doesn't conform to JSON standard so can't be parsed. – Aaron Greenwald May 15 '15 at 15:01
  • @aarong yes it doesn't answer the question as i said in the comment above yours. I don't see why you're complaining, it has upvotes because it helped someone solve their problem. – Matej May 15 '15 at 15:04
  • @matejkramny Fair point, in my haste I didn't see your comment. Apologies. It may help many who have similar questions, and that's represented in the upvotes you have. I stand by my downvote, though, because the question specifies the string format and for those of us that have the same question as the OP this doesn't answer the question. – Aaron Greenwald May 15 '15 at 15:21
  • Understood @aarong. Not sure what the best step would be to correcting the issue (in the best interest of the community and knowledge base). See the meta question: https://meta.stackoverflow.com/questions/294414 – Matej May 15 '15 at 15:30
  • 1
    var a = "firstName:name1, lastName:last1"; JSON.parse('{' + a + '}') throws an error. – Aaron Greenwald May 15 '15 at 16:01
  • 1
    @TravisJ Your comment is just as wrong as this answer. For `JSON.parse` to work, you'd need to be parsing the input `'{"firstName":"name1", "lastName":"last1"}'`. Note all the additional quotes. – Louis May 15 '15 at 17:00
  • 1
    @Louis - Yes, each piece would need to be quoted for it to comply to JSON. If doing that is non trivial then it would be just as hard to wrap the values in quotes as it would to parse the properties manually. I removed my prior comment. – Travis J May 15 '15 at 17:09
  • Everyone knows how to convert stringified JSON to JSON. This question answers an entirely different question. The question is how to convert a string that doesn't represent valid JSON into an object. – Derek Henderson May 26 '16 at 10:33
  • This works fine for me though I'm still not sure why string text is recognised as object when parsing and need to stringify first... – Yuki Tanaka Jun 18 '20 at 01:07
  • point 2 does not work (pasted into the console of the chrome browser and pressed enter) – Ruslan Apr 01 '22 at 11:49
  • Your quoting is different than the question. – johny why Dec 04 '22 at 11:07
93

Your string looks like a JSON string without the curly braces.

This should work then:

obj = eval('({' + str + '})');

WARNING: this introduces significant security holes such as XSS with untrusted data (data that is entered by the users of your application.)

fatal_error
  • 5,457
  • 2
  • 18
  • 18
Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
  • 3
    JSON requires quotations around key values, doesn't it? – cdleary Jul 06 '09 at 10:46
  • 31
    this is a potential security hole. I wouldn't reccomend this method at all. – Breton Jul 06 '09 at 14:16
  • 91
    It depends where the data comes from. Some people are obsessed with security. If you control your data, you can be more pragmatic in finding solutions. Or do you have a burglar-proof door between your kitchen and living room? – Philippe Leybaert Jul 07 '09 at 06:35
  • 14
    That's not working. It gives you error **'SyntaxError: Unexpected token :'**. I've checked it in the Chrome. – Nyambaa May 18 '11 at 08:09
  • 12
    The solution needs parentheses around it: `obj = eval('({' + str + '})');` – Christian Jun 14 '13 at 18:03
  • need to use regex man to conver it into json then parse it into object. – Muhammad Umer Jul 18 '13 at 23:09
  • 2
    Why on earth would you use eval for this? Its 1: inefficient 2: insecure 3: unreadable. See my answer @ http://stackoverflow.com/a/17597365/666835 – Matej Sep 19 '13 at 17:04
  • 1
    eval() is built-in. JSON.parse() is not. – Philippe Leybaert Sep 19 '13 at 19:02
  • 5
    Everyone who defends this answer should get minus 10k reputation. Eval is a bad function one should NEVER use. I'm not even talking about the security issues here. One error in the code you try to eval and you will have one hell of debugging. window.JSON is widely support since quite some time: http://caniuse.com/json Most JS frameworks also support JSON parsing. –  Oct 16 '13 at 14:31
  • 12
    prc322: Everyone knows that eval() is not very secure (and that's an understatement), but using eval() is the only correct answer to the question above because the input string is not a valid JSON string AND the input string references other variables by name. – Philippe Leybaert Oct 16 '13 at 21:20
  • 4
    Can anyone explain why eval is a security hole in JS when using console (Chrome) the user could potentially run code whenever they wanted on the browser.. unless of course its used in Node.JS etc. – Dean Meehan Apr 02 '14 at 08:54
  • I finally down-voted in 2014. This is due to increased restrictions on eval in various environments and tooling; not just a general dislike (which I do have) for eval for this purpose. Also, answers should guard themselves and point out limitations or issues. – user2864740 Jan 13 '15 at 18:34
  • @DeanMeehan `eval` allows for the potential abuse of XSS, depending where the string is sourced; even assuming that the local system is itself secure. There is a difference between the user *running JS on their own behalf* and code being injected into a different context. Putting a general "don't use" restriction on `eval` removes a source for another potential exploitation vector. (Granted that many sites still don't safely even generate HTML or JS-in-HTML, YMMV.) – user2864740 Jan 13 '15 at 18:35
  • 2
    -1: `eval` prevents the JavaScript compiler from doing optimizations. The engine can only know at runtime since it cheats the lexical scope. – Alerty Mar 01 '15 at 01:42
  • The OP's string does **NOT** look like "a JSON string without the curly braces."*. Moreover, `eval`'ing it will throw a `ReferenceError` unless `name1` and `last1` are defined in-scope identifiers. – T.J. Crowder Sep 19 '16 at 17:36
  • @ChristianKernstock Do you know how many downvotes that would take? – bb216b3acfd8f72cbc8f899d4d6963 Dec 23 '16 at 19:18
  • `eval()` is not a solution. What's going to happen when the input collides with JS functionality? This isn't just about security, it's about properly working software. It's about separating *data* from the *command*. Without that separation, you're basically asking for broken software. – Brad May 15 '17 at 20:57
  • `eval()` is the ONLY correct solution provided here. That is going to work without any interaction with the input string. `JSON.parse` won't work because there is no quotes around properties. Any splitting by `,` or `:` won't work because what if it's included in some property or value? Sorry guys :) @PhilippeLeybaert is right here. What might work and won't be `security hole` could be a custom parser that will watch for all closing/open/nested quotes/brackets and so on. Possible to do. Worth it? Depends. – Papi Jan 14 '19 at 10:49
  • be careful when using eval it has a lot of issues in javascript – Yazan Najjar Jul 13 '20 at 22:35
66

If I'm understanding correctly:

var properties = string.split(', ');
var obj = {};
properties.forEach(function(property) {
    var tup = property.split(':');
    obj[tup[0]] = tup[1];
});

I'm assuming that the property name is to the left of the colon and the string value that it takes on is to the right.

Note that Array.forEach is JavaScript 1.6 -- you may want to use a toolkit for maximum compatibility.

cdleary
  • 69,512
  • 53
  • 163
  • 191
  • Hey Cdleary... i wonder if you can help me: http://stackoverflow.com/questions/9357320/garbage-collector-issues-on-spidermonkey-js-anchorptr Sorry couldnt find another way to contact you except thru comments on ur answers – Jason Feb 21 '12 at 06:46
  • 1
    Prefect post. This answer uses the basics of JavaScript very clear and should thereby works in every browser – Michel Jan 24 '13 at 11:19
  • 1
    I found this approach was a good start, so the answer was useful. However, xecute has a valid point that it doesn't accommodate strings that may have commas. You could go down the path of matching quotes if your code needs to handle strings with commas, but you still wont catch cases where the quotes are escaped. Just implement what you need, then stop. – Patrick Graham May 13 '15 at 16:14
  • 1
    In a question of parsing text presented by the asker, questions of *"what if"* are useless. We can accept the given example as representative of the data or we can simply not answer since there could always be a *"what if"* that will break the parsing. –  Nov 21 '15 at 17:10
  • 1
    The poster asked about how to parse an unusual structure that was not valid JSON. @cdleary answered the question very well under the circumstances. xecute: handling a comma in a string would require a quoting or escaping mechanism, beyond the scope of this discussion. – Suncat2000 Dec 08 '16 at 19:26
  • best answer that actually answers the broader question and not the specific case that your string happens to be formatted as JSON... – Andrew Feb 15 '17 at 14:03
50

This simple way...

var string = "{firstName:'name1', lastName:'last1'}";
eval('var obj='+string);
alert(obj.firstName);

output

name1
Wianto Wie
  • 573
  • 4
  • 2
  • Upvote for this, ALTHOUGH: var string = "{firstName:'name1', lastName:'last1', f: function(){alert('u got hacked...')}()}"; eval('var obj'+string) This could get you hacked... but I think its worth while because its the only thing that works in some cases. – Cody Apr 29 '13 at 08:45
  • 1
    This was the only solution that could directly solve the question, although [using eval is dangerous](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval). – Hosana Gomes Mar 25 '21 at 15:58
  • Currently, any JS code executed in the browser is dangerous. Let's be frank... This answer is the only solution that could effectively solve the question "string to object in JS". `JSON.parse()` is limited to the scope of JSON. Remembering that a JSON is not a JS Object. Thanks! =D – Eduardo Lucio May 22 '21 at 00:00
29

Since JSON.parse() method requires the Object keys to be enclosed within quotes for it to work correctly, we would first have to convert the string into a JSON formatted string before calling JSON.parse() method.

var obj = '{ firstName:"John", lastName:"Doe" }';

var jsonStr = obj.replace(/(\w+:)|(\w+ :)/g, function(matchedStr) {
  return '"' + matchedStr.substring(0, matchedStr.length - 1) + '":';
});

obj = JSON.parse(jsonStr); //converts to a regular object

console.log(obj.firstName); // expected output: John
console.log(obj.lastName); // expected output: Doe

This would work even if the string has a complex object (like the following) and it would still convert correctly. Just make sure that the string itself is enclosed within single quotes.

var strObj = '{ name:"John Doe", age:33, favorites:{ sports:["hoops", "baseball"], movies:["star wars", "taxi driver"]  }}';

var jsonStr = strObj.replace(/(\w+:)|(\w+ :)/g, function(s) {
  return '"' + s.substring(0, s.length-1) + '":';
});

var obj = JSON.parse(jsonStr);
console.log(obj.favorites.movies[0]); // expected output: star wars
Faisal Chishti
  • 301
  • 3
  • 4
  • 1
    This is the closest solution without using eval() and almost works with all JS Objects in String. But beware, that this solution is not working with timestamps as values! i.e. {from:"2021-05-09T22:00:00.000Z"} is probably converted as {"from":"2021-05-09T"22":"00":00.000Z"} – Pivoman May 16 '21 at 13:56
  • Your quoting is different than the question. – johny why Dec 04 '22 at 11:05
18

If you have a string like foo: 1, bar: 2 you can convert it to a valid obj with:

str
  .split(',')
  .map(x => x.split(':').map(y => y.trim()))
  .reduce((a, x) => {
    a[x[0]] = x[1];
    return a;
  }, {});

Thanks to niggler in #javascript for that.

Update with explanations:

const obj = 'foo: 1, bar: 2'
  .split(',') // split into ['foo: 1', 'bar: 2']
  .map(keyVal => { // go over each keyVal value in that array
    return keyVal
      .split(':') // split into ['foo', '1'] and on the next loop ['bar', '2']
      .map(_ => _.trim()) // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
  })
  .reduce((accumulator, currentValue) => { // reduce() takes a func and a beginning object, we're making a fresh object
    accumulator[currentValue[0]] = currentValue[1]
    // accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
    // since reduce() works like map() in the sense it iterates over an array, and it can be chained upon things like map(),
    // first time through it would say "okay accumulator, accumulate currentValue[0] (which is 'foo') = currentValue[1] (which is '1')
    // so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
    // second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
    return accumulator
  }, {}) // when there are no more things in the array to iterate over, it returns the accumulated stuff

console.log(obj)

Confusing MDN docs:

Demo: http://jsbin.com/hiduhijevu/edit?js,console

Function:

const str2obj = str => {
  return str
    .split(',')
    .map(keyVal => {
      return keyVal
        .split(':')
        .map(_ => _.trim())
    })
    .reduce((accumulator, currentValue) => {
      accumulator[currentValue[0]] = currentValue[1]
      return accumulator
    }, {})
}

console.log(str2obj('foo: 1, bar: 2')) // see? works!
corysimmons
  • 7,296
  • 4
  • 57
  • 65
  • This is a totally incomprehensible answer for the average reader. Can you explain what each lines does? And what is the resulting output, given OP's input? – not2qubit Jan 31 '18 at 11:17
  • 1
    Fair enough. Typically I don't have time to detail stuff and I'm just dropping helpful bits (specifically in case I run into the same exact SO question down the road), but I gotchu. – corysimmons Jan 31 '18 at 15:40
  • 5
    This is definitely the most elegant solution and actually answers the question unlike a lot of the other answers. And thank you for the explanation! – Cathy Ha Aug 14 '19 at 04:31
  • This will only work for simple object values. If we enter things like "http://google.com" it will break. – Heri Hehe Setiawan Apr 22 '22 at 06:55
  • Does not work for nested objects. Try this: str2obj( 'x:10, y: {z: 10}' ) – Mareș Ștefan Apr 22 '22 at 14:45
13

You need use JSON.parse() for convert String into a Object:

var obj = JSON.parse('{ "firstName":"name1", "lastName": "last1" }');
GrigorAtaryan
  • 445
  • 4
  • 10
11

if you're using JQuery:

var obj = jQuery.parseJSON('{"path":"/img/filename.jpg"}');
console.log(obj.path); // will print /img/filename.jpg

REMEMBER: eval is evil! :D

mzalazar
  • 6,206
  • 3
  • 34
  • 31
  • 7
    jQuery uses `eval`. `globalEval: /** code **/ window[ "eval" ].call( window, data ); /** more code **/` – SomeShinyObject May 08 '13 at 01:12
  • 12
    The string, provided by question owner is not a valid JSON string. So, this code is useless... – xecute Oct 31 '13 at 15:27
  • yes, eval is not evil if you use it on a "controlled" environment (anything but external data like files, network or user input, in that cases it could be dangerous if not "filtered/validated"). – mzalazar Sep 16 '16 at 08:02
5

I implemented a solution in a few lines of code which works quite reliably.

Having an HTML element like this where I want to pass custom options:

<div class="my-element"
    data-options="background-color: #dadada; custom-key: custom-value;">
</div>

a function parses the custom options and return an object to use that somewhere:

function readCustomOptions($elem){
    var i, len, option, options, optionsObject = {};

    options = $elem.data('options');
    options = (options || '').replace(/\s/g,'').split(';');
    for (i = 0, len = options.length - 1; i < len; i++){
        option = options[i].split(':');
        optionsObject[option[0]] = option[1];
    }
    return optionsObject;
}

console.log(readCustomOptions($('.my-element')));
rodu
  • 2,308
  • 3
  • 15
  • 6
  • +1 for using the `data-` attribute instead of creating pseudo-custom attributes like some frameworks/libraries. – John Aug 21 '18 at 03:38
5

I'm using JSON5, and it's works pretty well.

The good part is it contains no eval and no new Function, very safe to use.

James Yang
  • 476
  • 5
  • 6
5

In your case, The short and beautiful code

Object.fromEntries(str.split(',').map(i => i.split(':')));
ben
  • 71
  • 1
  • 4
3
string = "firstName:name1, lastName:last1";

This will work:

var fields = string.split(', '),
    fieldObject = {};

if( typeof fields === 'object') ){
   fields.each(function(field) {
      var c = property.split(':');
      fieldObject[c[0]] = c[1];
   });
}

However it's not efficient. What happens when you have something like this:

string = "firstName:name1, lastName:last1, profileUrl:http://localhost/site/profile/1";

split() will split 'http'. So i suggest you use a special delimiter like pipe

 string = "firstName|name1, lastName|last1";


   var fields = string.split(', '),
        fieldObject = {};

    if( typeof fields === 'object') ){
       fields.each(function(field) {
          var c = property.split('|');
          fieldObject[c[0]] = c[1];
       });
    }
Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96
  • 2
    Just an idea - instead of second split(':') you could split the string "manually" by FIRST colon using indexOf(":") and consider the part of the string until this colon as key and the rest after the colon as the value. – Ondrej Vencovsky Nov 11 '15 at 14:26
  • http doesn't appear in the question. – johny why Dec 04 '22 at 11:07
2

This is universal code , no matter how your input is long but in same schema if there is : separator :)

var string = "firstName:name1, lastName:last1"; 
var pass = string.replace(',',':');
var arr = pass.split(':');
var empty = {};
arr.forEach(function(el,i){
  var b = i + 1, c = b/2, e = c.toString();
     if(e.indexOf('.') != -1 ) {
     empty[el] = arr[i+1];
  } 
}); 
  console.log(empty)
panatoni
  • 735
  • 6
  • 13
2
const text = '{"name":"John", "age":30, "city":"New York"}';
const myArr = JSON.parse(text);
document.getElementById("demo").innerHTML = myArr.name;
solice
  • 77
  • 3
1

Here is my approach to handle some edge cases like having whitespaces and other primitive types as values

const str = " c:234 , d:sdfg ,e: true, f:null, g: undefined, h:name "; 

const strToObj = str
  .trim()
  .split(",")
  .reduce((acc, item) => {
    const [key, val = ""] = item.trim().split(":");
    let newVal = val.trim();

    if (newVal == "null") {
      newVal = null;
    } else if (newVal == "undefined") {
      newVal = void 0;
    } else if (!Number.isNaN(Number(newVal))) {
      newVal = Number(newVal);
    }else if (newVal == "true" || newVal == "false") {
      newVal = Boolean(newVal);
    }
    return { ...acc, [key.trim()]: newVal };
  }, {});
Akash Singh
  • 547
  • 5
  • 8
0

In your case

var KeyVal = string.split(", ");
var obj = {};
var i;
for (i in KeyVal) {
    KeyVal[i] = KeyVal[i].split(":");
    obj[eval(KeyVal[i][0])] = eval(KeyVal[i][1]);
}
Vahag Chakhoyan
  • 873
  • 1
  • 10
  • 21
0
var stringExample = "firstName:name1, lastName:last1 | firstName:name2, lastName:last2";    

var initial_arr_objects = stringExample.split("|");
    var objects =[];
    initial_arr_objects.map((e) => {
          var string = e;
          var fields = string.split(','),fieldObject = {};
        if( typeof fields === 'object') {
           fields.forEach(function(field) {
              var c = field.split(':');
              fieldObject[c[0]] = c[1]; //use parseInt if integer wanted
           });
        }
            console.log(fieldObject)
            objects.push(fieldObject);
        });

"objects" array will have all the objects

0

As already mentioned:

  • JSON.parse does not accept unquoted keys.
  • JSON5 is very robust

My requirements:

  • JSON5's bundle size is 31.82KB -> 9.77KB (gzip). I needed something more lightweight
  • I needed to process any user-defined string. You may not need this.

Function:

export const deserialize = <R>(arg?: string | null): R => {

  // IS THE STRING NULL OR UNDEFINED?
  if (arg === null || arg === undefined) {
    return arg as R
  }

  // IS THE STRING 'undefined'?
  if (arg === 'undefined') {
    return undefined as R
  }

  // IS THE STRING EMPTY?
  if (arg === '') {
    return arg as R
  }

  // IS THE STRING A NUMBER?
  if (!isNaN(Number(arg))) {
    return parseFloat(arg) as R;
  }

  // IS THE STRING A BOOLEAN?
  if (arg === 'true') {
    return true as R
  }
  if (arg === 'false') {
    return false as R
  }

  // IS THE STRING JSON?
  try {
    const potentiallyParsableJson = arg
      // wrap all strings wrapped in single quotes with double quotes
      .replace(/'([^']+)'/g, '"$1"')
      // wrap all unquoted keys in double quotes
      .replace(/([{,]\s*)([a-zA-Z0-9_]+?)\s*:/g, '$1"$2":')
      // remove all trailing commas
      .replace(/,\s*}/g, '}').replace(/,\s*]/g, ']')

    return JSON.parse(potentiallyParsableJson)
  } catch (e) {

    // WE'VE RUN OUT OF OPTIONS, JUST RETURN THE STRING
    return arg as R
  }
}

Test cases:

describe('deserialize', () => {

  it('should handle null', () => {
    expect(deserialize(null)).toEqual(null);
  })

  it('should handle undefined', () => {
    expect(deserialize()).toEqual(undefined);
  })

  it(`should handle 'null'`, () => {
    expect(deserialize('null')).toEqual(null);
  })

  it(`should handle 'undefined'`, () => {
    expect(deserialize('undefined')).toEqual(undefined);
  })

  it('should handle empty strings', () => {
    expect(deserialize('')).toEqual('');
  })

  it('should handle normal strings', () => {
    expect(deserialize('test')).toEqual('test');
  })

  it('should handle numbers', () => {
    expect(deserialize('33')).toEqual(33);
  })

  it('should handle a value of "true"', () => {
    expect(deserialize('true')).toEqual(true);
  })

  it('should handle a value of "false"', () => {
    expect(deserialize('false')).toEqual(false);
  })

  it('should handle a shallow object', () => {
    expect(deserialize(`{ str: 'str', boo: true, num: 3 }`)).toEqual({ str: "str", boo: true, num: 3 });
  })

  it('should handle a deep object', () => {
    expect(deserialize(`{ str: 'str', boo: true, num: 3, arr: [1, 2, 3], obj: { one: { two: { three: 3 } } } }`))
      .toEqual({ str: "str", boo: true, num: 3, arr: [1, 2, 3], obj: { one: { two: { three: 3 } } } });
  })

  it('should simply return the same string if it is not valid JSON', () => {
    expect(deserialize(`{ str: str, boo: true }`)).toEqual(`{ str: str, boo: true }`);
  })

  it('arrays may have a tailing comma', () => {
    expect(deserialize(`{ arr: [1, 2, 3], }`)).toEqual({ arr: [1, 2, 3] })
  })

  it('objects may have a tailing comma', () => {
    expect(deserialize(`{ obj: {one: 'test'}, }`)).toEqual({ obj: { one: 'test' } })
  })

});
Stephen Paul
  • 37,253
  • 15
  • 92
  • 74
-1

I know this is an old post but didn't see the correct answer for the question.

var jsonStrig = '{';
      var items = string.split(',');
      for (var i = 0; i < items.length; i++) {
          var current = items[i].split(':');
          jsonStrig += '"' + current[0] + '":"' + current[1] + '",';
      }
      jsonStrig = jsonStrig.substr(0, jsonStrig.length - 1);
      jsonStrig += '}';
var obj = JSON.parse(jsonStrig);
console.log(obj.firstName, obj.lastName);

Now you can use obj.firstName and obj.lastName to get the values as you could do normally with an object.

Raoul
  • 188
  • 2
  • 8
-1

You don't have to always convert to JSON

So here "person begin as a string!" Finally, "person is converted to object", no necessarily to JSON.

function strToObj(e){if(typeof e=="string"){ let obj=new Function("return" +e); try{return obj()}catch{console.log("Fix, String no allowed to object")}}else{console.log("it is not a string") } };
//Example, person is a string
  let person='{firstName:"John", lastName:"Doe", id: 55, fullName:function(){return this.firstName+" "+this.lastName} }';
console.log(strToObj(person));

And it run functions internal to the object without major issues if it is called:

person=strToObj(person); console.log(person.fullName())

Simply, string = "firstName:name1, lastName:last1";

let string = "firstName:name1, lastName:last1"; 
let object= strToObj("{"+string+"}");
console.log(object)