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 :: what is == in js 
Javascript :: add new array at the back of react state 
Javascript :: array.splice 
Javascript :: pop-under window before current page 
Javascript :: get search value from reacr route1 
Javascript :: get search value from reacr route 
Javascript :: adding mui theme to index.js 
Javascript :: prisma where not in array 
Javascript :: sum up all the first and last digit of a number until only two digits are left 
Javascript :: import file in chrome extension 
Javascript :: jquery has class 
Javascript :: dropzone upload on one file 
Javascript :: nodejs module 
Javascript :: jquery find attribute from siblings 
Javascript :: localstorage getitem 
Javascript :: look behind regex 
Javascript :: ex: splide carousel 
Javascript :: usecontext 
Javascript :: node express config file json 
Javascript :: dataset javascript 
Javascript :: Split string on the first white space occurrence 
Javascript :: exponential javascript 
Javascript :: alpine js x-on click not working 
Javascript :: how to manually trigger browser back button from angular 
Javascript :: convert json to 2d array 
Javascript :: js run npm 
Javascript :: react-beforeunload react navive 
Javascript :: start button js 
Javascript :: react electron desktop app 
Javascript :: javascript xpath 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =