Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 23, 2023 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/e4760b14766fcb2060f6766e4a36c32b to your computer and use it in GitHub Desktop.
Save code-boxx/e4760b14766fcb2060f6766e4a36c32b to your computer and use it in GitHub Desktop.
Convert String To Object In Javascript

JAVASCRIPT CONVERT STRING TO OBJECT

https://code-boxx.com/convert-string-object-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// (A) JSON ENCODED STRING
var thestring = '{"Name":"Jon Doe","Email":"jon@doe.com","Address":"123 Doe Street"}';
// (B) PARSE JSON
/*
Name: "Jon Doe",
Email: "jon@doe.com",
Address: "123 Doe Street"
*/
var theobj = JSON.parse(thestring);
console.log(theobj);
// (A) CSV STRING
var thestring = "Name,Joe Doe,Email,joe@doe.com,Address,234 Doe Street";
// (B) MANUAL PROCESS AND LOOP
/*
Name: "Joe Doe",
Email: "joe@doe.com",
Address: "234 Doe Street"
*/
var temp = thestring.split(","), theobj = {};
for (let i=0; i<temp.length; i+=2) {
theobj[temp[i]] = temp[(i+1)];
}
console.log(theobj);
// (A) THE EXISTING OBJECT
var theobj = {
// (A1) OBJECT PROPERTIES
Name: null,
Email: null,
Address: null,
// (A2) IMPORT FROM CSV STRING
inCSV : str => {
let tempA = str.split(","), tempB = {};
for (let i=0; i<tempA.length; i+=2) {
tempB[tempA[i]] = tempA[(i+1)];
}
theobj.Name = tempB.Name;
theobj.Email = tempB.Email;
theobj.Address = tempB.Address;
},
// (A3) IMPORT FROM JSON STRING
inJSON : str => {
let temp = JSON.parse(str);
theobj.Name = temp.Name;
theobj.Email = temp.Email;
theobj.Address = temp.Address;
}
};
// (B) PROCESS "IMPORT"
// (B1) CSV
/*
Name: "Job Doe",
Email: "job@doe.com",
Address: "345 Doe Street"
*/
var dataA = "Name,Job Doe,Email,job@doe.com,Address,345 Doe Street";
theobj.inCSV(dataA);
console.log(theobj);
// (B2) JSON
/*
Name: "Joy Doe",
Email: "joy@doe.com",
Address: "456 Doe Street"
*/
var dataB = '{"Name":"Joy Doe","Email":"joy@doe.com","Address":"456 Doe Street"}';
theobj.inJSON(dataB);
console.log(theobj);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment