Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

convert string to object javascript

var mystr = '{ "hello":"world" }' // NB: Has enclosing ""
var myobj = JSON.parse(mystr);
Comment

convert a string to object javascript

var mystr = '{ "hello":"world" }' // NB: Has enclosing ""
var myobj = JSON.parse(mystr);

console.log(myobj);

result:
{
  hello: "world"
}
Comment

how to convert string to object in JS??

const string = '{"name":"paresh","age":34,"professional":"coder"}';
const obj=JSON.parse(string)
Comment

convert string to object javascript

str = "firstName:name1, lastName:last1"; // NB: No enclosing ""
obj = eval('({' + str + '})');
Comment

string object to object in js

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
 Run code snippet
Comment

string object to object in js

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
 Run code snippet
Comment

string to object js

let arr = [
    "{"Machine Name":"Armature Assy 1","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 2","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 3","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 4","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 5","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 6","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 7","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 8","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 9","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 10","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 11","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 12","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 13","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 14","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 15","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 16","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 17","STS AM":5.5,"Waiting Time AM":0,"Breakdown AM":24.3,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 18","STS AM":1.1,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 19","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 20","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 21","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 22","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 23","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 24","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 27","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 28","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 29","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 30","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 31","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}",
    "{"Machine Name":"Armature Assy 99","STS AM":0,"Waiting Time AM":0,"Breakdown AM":0,"STS PM":0,"Waiting Time PM":0,"Breakdown PM":0}"
]
console.log(arr.map(x=> JSON.parse(x)))
Comment

PREVIOUS NEXT
Code Example
Javascript :: npm get package.json version 
Javascript :: global axios vue 2 
Javascript :: cors blocking communication 
Javascript :: Reusable Alpine.js components 
Javascript :: vuetify open modal based on url anchor or # 
Javascript :: [W] undefined:undefined - Ruleset uses old version (version [1]). Please update to the latest version (version [2]). 
Javascript :: express.js hello world 
Javascript :: socket..io 
Javascript :: paragraph antd 
Javascript :: print array without brackets javascript 
Javascript :: tostring js 
Javascript :: remove array item with index 
Javascript :: merge objects javascript es6 
Javascript :: mongoose connection increase timeout in node js 
Javascript :: convert html to pdf using javascript 
Javascript :: for of javascript 
Javascript :: create javascript array with 1 value 
Javascript :: angular tab change smooth transition 
Javascript :: extract domain from url js 
Javascript :: array javascript 
Javascript :: Object.values returns 
Javascript :: js event div class adding 
Javascript :: setImmediate() nodejs 
Javascript :: display for sometime only session flash message in laravel with javascript 
Javascript :: CSRF token in js 
Javascript :: null data type in javascript 
Javascript :: jquery for element which doesnt exist on page load 
Javascript :: redux reducer 
Javascript :: javascript datatypes 
Javascript :: javascript timestamp conversion 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =