get JSONS from a single string - javascript

i get a single String value as
"{"Link":"","DefaultValue":"","Content":"LONDON"}, {"Link":"","DefaultValue":"","Content":"United Kingdom"}"
which contains two jsons . how can i get each json and put in into an array or something in javascript/jquery ?
Please suggest the best way.

The String that you posted isn't valid JSON format. If it is an array of two objects, it should read:
'[{"Link":"","DefaultValue":"","Content":"LONDON"}, {"Link":"","DefaultValue":"","Content":"United Kingdom"}]'
Note the single quotes at the beginning and end, so that Javascript doesn't confuse the double quotes in the JSON and can parse them correctly.
Also note the brackets [] around the whole thing, that tells the parser that it is an array of objects.
You can read the new string into an object array like this:
var str = '[{"Link":"","DefaultValue":"","Content":"LONDON"}, {"Link":"","DefaultValue":"","Content":"United Kingdom"}]';
var arr = JSON.parse(str);

Related

How to convert string to object in javascript?

const str="{a:{url:'http://localhost:80',c:1,},d:'d',e:true}"
How to get the result without using evil and new Function:
const obj={a:{url:'http://localhost:80',c:1,},d:'d',e:true}
First of all:
You need to format your string into a valid JSON format ( double quotes instead of single quotes )
And then you'll just need to JSON.parse(str)
To convert a string to json you can use JSON.parse(str)
But first make sure the string syntax is correct, there are several websites to do this, one of many is this https://jsonlint.com/

parse a string that contains array with strings

I am struggling to parse a string that contains array. And the array also contains list of arrays.
Each of the array contains string.
code here
let a = "[[48934, 'Danial Brendon', 'developer'],[48934, 'Nicki Lopez', 'developer']]";
console.log(JSON.parse(a))
I tried using JSON.parse() but did not work, may be because JSON.parse() also want to parse the string.
I am having difficulty with this even this looks simple. I could not find any similar question/answer like this.
Thanks.
To JSON parse , you need double quotes instead of single. like this ...
let a = '[[48934, "Danial Brendon", "developer"],[48934, "Nicki Lopez", "developer"]]';
console.log(JSON.parse(a));

Convert String of certain format to JSON in javascript

I have a string of the format
var str = "{key1=value1, Key2=value2}"
I need to convert this into a json object to be able to iterate through it.
Any suggestions on how this can be done? there can be any number of keys
You need first to "JSONize" this string you are getting so it can be converted to a JavaScript object using the JSON class. My guess, if the string has always this format ({key=value, ...}), is that you could parse it first like this:
var parsedString = yourString.replace(/(\b\S+\b)=(\b\S+\b)/g, '"$1":"$2"')
This way, from this: "{key1=value1, Key2=value2}" you get this: '{"key1":"value1", "Key2":"value2"}'.
Then, as someone suggested, just use JSON.parse(parsedString) to get your JS object.

correctly adding to json from javascript

hi im having trouble correctly adding to my json
here is the code.
When i console.log the string im trying to add is
{"type":"#","name":"wh2xogvi","list":[{"0":"background-color"},{"1":"border"},{"2":"width"}, {"3":"height"},{"4":"margin"}],"listvalues":[{"0":"#aaa"},{"1":"2px solid #000"},{"2":"1040px"},{"3":"50px"},{"4":"0 auto"}]}
it is valid json
var jsonltoload = JSON.stringify(eval("(" + jsonloadtostring + ")"));
console.log(jsonltoload); // this is the console log i was talking about higher up
fullJSON.styles.objectcss.push(jsonltoload);
But when i actually look at the json it is wrong ends up something like this
"{\"type\":\"#\",\"name\":\"unkd42t9\",\"list\":[{\"0\":\"background-color\"},{\"1\":\"border\"},{\"2\":\"width\"},{\"3\":\"height\"},{\"4\":\"clear\"}],\"listvalues\":[{\"0\":\"#ddd\"},{\"1\":\"2px solid #000\"},{\"2\":\"100%\"},{\"3\":\"50px\"},{\"4\":\"both\"}]}",
the fullJSON comes from JSON.parse(json); which comes from a file
You seem to confuse JSON, a textual, language-independent data representation, with JavaScript objects, a language-specific data type.
JSON.stringify returns a string (containing JSON), so jsonltoload is a string. I guess you simply want to parse the JSON and add the resulting object:
var obj = JSON.parse(jsonloadtostring);
fullJSON.styles.objectcss.push(obj);
I think the JSON string is trying to escape the double quotes character you have added to string, resulting in the string. try to enclose the whole string with single quotes rather than double quotes

How can I parse this string into an array?

I have a string representation of the following array, generated from Signature Pad:
var myData = "[{lx:47,ly:28,mx:47,my:27},{lx:47,ly:32,mx:47,my:28},{lx:47,ly:40,mx:47,my:32},{lx:48,ly:50,mx:47,my:40},{lx:49,ly:59,mx:48,my:50},{lx:49,ly:66,mx:49,my:59},{lx:51,ly:72,mx:49,my:66},{lx:54,ly:76,mx:51,my:72},{lx:56,ly:76,mx:54,my:76},{lx:58,ly:76,mx:56,my:76},{lx:59,ly:76,mx:58,my:76},{lx:61,ly:76,mx:59,my:76},{lx:62,ly:76,mx:61,my:76},{lx:64,ly:76,mx:62,my:76},{lx:66,ly:73,mx:64,my:76},{lx:70,ly:69,mx:66,my:73},{lx:73,ly:64,mx:70,my:69},{lx:75,ly:61,mx:73,my:64},{lx:79,ly:56,mx:75,my:61},{lx:82,ly:51,mx:79,my:56},{lx:84,ly:46,mx:82,my:51},{lx:85,ly:43,mx:84,my:46},{lx:87,ly:40,mx:85,my:43},{lx:88,ly:35,mx:87,my:40},{lx:90,ly:34,mx:88,my:35},{lx:92,ly:33,mx:90,my:34},{lx:93,ly:32,mx:92,my:33},{lx:94,ly:32,mx:93,my:32},{lx:96,ly:33,mx:94,my:32},{lx:96,ly:35,mx:96,my:33},{lx:99,ly:37,mx:96,my:35},{lx:101,ly:42,mx:99,my:37},{lx:101,ly:46,mx:101,my:42},{lx:101,ly:50,mx:101,my:46},{lx:101,ly:54,mx:101,my:50},{lx:102,ly:57,mx:101,my:54},{lx:104,ly:58,mx:102,my:57},{lx:105,ly:59,mx:104,my:58},{lx:107,ly:60,mx:105,my:59},{lx:108,ly:60,mx:107,my:60},{lx:109,ly:60,mx:108,my:60},{lx:110,ly:60,mx:109,my:60},{lx:112,ly:58,mx:110,my:60},{lx:114,ly:57,mx:112,my:58},{lx:116,ly:54,mx:114,my:57},{lx:119,ly:53,mx:116,my:54},{lx:120,ly:50,mx:119,my:53},{lx:123,ly:49,mx:120,my:50},{lx:127,ly:48,mx:123,my:49},{lx:130,ly:48,mx:127,my:48},{lx:132,ly:48,mx:130,my:48},{lx:134,ly:49,mx:132,my:48},{lx:136,ly:50,mx:134,my:49},{lx:137,ly:52,mx:136,my:50},{lx:139,ly:56,mx:137,my:52},{lx:140,ly:59,mx:139,my:56},{lx:140,ly:60,mx:140,my:59},{lx:143,ly:61,mx:140,my:60},{lx:144,ly:61,mx:143,my:61},{lx:146,ly:61,mx:144,my:61},{lx:151,ly:61,mx:146,my:61},{lx:156,ly:61,mx:151,my:61},{lx:161,ly:61,mx:156,my:61},{lx:167,ly:60,mx:161,my:61},{lx:173,ly:60,mx:167,my:60},{lx:178,ly:60,mx:173,my:60},{lx:185,ly:60,mx:178,my:60},{lx:192,ly:60,mx:185,my:60},{lx:198,ly:60,mx:192,my:60},{lx:207,ly:60,mx:198,my:60},{lx:214,ly:60,mx:207,my:60},{lx:221,ly:60,mx:214,my:60},{lx:226,ly:60,mx:221,my:60},{lx:229,ly:60,mx:226,my:60},{lx:233,ly:60,mx:229,my:60},{lx:234,ly:60,mx:233,my:60},{lx:235,ly:60,mx:234,my:60},{lx:237,ly:60,mx:235,my:60},{lx:238,ly:60,mx:237,my:60},{lx:239,ly:60,mx:238,my:60},{lx:241,ly:60,mx:239,my:60},{lx:244,ly:60,mx:241,my:60},{lx:245,ly:60,mx:244,my:60},{lx:246,ly:60,mx:245,my:60},{lx:248,ly:59,mx:246,my:60},{lx:248,ly:58,mx:248,my:59},{lx:248,ly:57,mx:248,my:58}]";
I tried to convert it to an array:
JSON.parse(myData);
But I get the following error message:
SyntaxError: Unexpected token l
What am I doing wrong? I can't see the mistake:( I tried pasting the exact same data without the quotes in the console and it creates an array:
var myData = [{lx:47,ly:28,mx:47,my:27},{lx:47,ly:32,mx:47,my:28},{lx:47,ly:40,mx:47,my:32},{lx:48,ly:50,mx:47,my:40},{lx:49,ly:59,mx:48,my:50},{lx:49,ly:66,mx:49,my:59},{lx:51,ly:72,mx:49,my:66},{lx:54,ly:76,mx:51,my:72},{lx:56,ly:76,mx:54,my:76},{lx:58,ly:76,mx:56,my:76},{lx:59,ly:76,mx:58,my:76},{lx:61,ly:76,mx:59,my:76},{lx:62,ly:76,mx:61,my:76},{lx:64,ly:76,mx:62,my:76},{lx:66,ly:73,mx:64,my:76},{lx:70,ly:69,mx:66,my:73},{lx:73,ly:64,mx:70,my:69},{lx:75,ly:61,mx:73,my:64},{lx:79,ly:56,mx:75,my:61},{lx:82,ly:51,mx:79,my:56},{lx:84,ly:46,mx:82,my:51},{lx:85,ly:43,mx:84,my:46},{lx:87,ly:40,mx:85,my:43},{lx:88,ly:35,mx:87,my:40},{lx:90,ly:34,mx:88,my:35},{lx:92,ly:33,mx:90,my:34},{lx:93,ly:32,mx:92,my:33},{lx:94,ly:32,mx:93,my:32},{lx:96,ly:33,mx:94,my:32},{lx:96,ly:35,mx:96,my:33},{lx:99,ly:37,mx:96,my:35},{lx:101,ly:42,mx:99,my:37},{lx:101,ly:46,mx:101,my:42},{lx:101,ly:50,mx:101,my:46},{lx:101,ly:54,mx:101,my:50},{lx:102,ly:57,mx:101,my:54},{lx:104,ly:58,mx:102,my:57},{lx:105,ly:59,mx:104,my:58},{lx:107,ly:60,mx:105,my:59},{lx:108,ly:60,mx:107,my:60},{lx:109,ly:60,mx:108,my:60},{lx:110,ly:60,mx:109,my:60},{lx:112,ly:58,mx:110,my:60},{lx:114,ly:57,mx:112,my:58},{lx:116,ly:54,mx:114,my:57},{lx:119,ly:53,mx:116,my:54},{lx:120,ly:50,mx:119,my:53},{lx:123,ly:49,mx:120,my:50},{lx:127,ly:48,mx:123,my:49},{lx:130,ly:48,mx:127,my:48},{lx:132,ly:48,mx:130,my:48},{lx:134,ly:49,mx:132,my:48},{lx:136,ly:50,mx:134,my:49},{lx:137,ly:52,mx:136,my:50},{lx:139,ly:56,mx:137,my:52},{lx:140,ly:59,mx:139,my:56},{lx:140,ly:60,mx:140,my:59},{lx:143,ly:61,mx:140,my:60},{lx:144,ly:61,mx:143,my:61},{lx:146,ly:61,mx:144,my:61},{lx:151,ly:61,mx:146,my:61},{lx:156,ly:61,mx:151,my:61},{lx:161,ly:61,mx:156,my:61},{lx:167,ly:60,mx:161,my:61},{lx:173,ly:60,mx:167,my:60},{lx:178,ly:60,mx:173,my:60},{lx:185,ly:60,mx:178,my:60},{lx:192,ly:60,mx:185,my:60},{lx:198,ly:60,mx:192,my:60},{lx:207,ly:60,mx:198,my:60},{lx:214,ly:60,mx:207,my:60},{lx:221,ly:60,mx:214,my:60},{lx:226,ly:60,mx:221,my:60},{lx:229,ly:60,mx:226,my:60},{lx:233,ly:60,mx:229,my:60},{lx:234,ly:60,mx:233,my:60},{lx:235,ly:60,mx:234,my:60},{lx:237,ly:60,mx:235,my:60},{lx:238,ly:60,mx:237,my:60},{lx:239,ly:60,mx:238,my:60},{lx:241,ly:60,mx:239,my:60},{lx:244,ly:60,mx:241,my:60},{lx:245,ly:60,mx:244,my:60},{lx:246,ly:60,mx:245,my:60},{lx:248,ly:59,mx:246,my:60},{lx:248,ly:58,mx:248,my:59},{lx:248,ly:57,mx:248,my:58}];
Strings are required here. Therefore use "lx" instead of lx
var myData = '[{"lx":47,"ly":28 // and so on
The JSON specification is more strict than JavaScript itself. Specifically, JSON requires all object keys to be enclosed in double quotes. In the JSON spec, an object key must be a string token, and a string token begins and ends with a double quote character.
Thus, you'll need to quote your object keys:
var myData = '[{"lx":47,"ly":28,"mx":47,"my":27},...'
EDIT: According to the Signature Pad API, you can use getSignatureString() to serialize your pad into a valid JSON string.
Your json is not valid. Object property names should be a string so you need to use quotes
var myData = '[{"lx":47,"ly":28 ....}
Visit json.org for more information about this topic
The fact, that your string contains a valid javascript object (note: not JSON object) lets you take advantage of the evil eval function. Just evaluate your string and store its value in a variable like this:
var myData = eval("[{lx:47,ly:28,mx:47,my:27}]");
I made a fiddle for you to see that it works with the string you provided here
the variable myData has incorrect JSON format, to able to use JSON parse you need to add double quotes like here:
var myData = '[{"lx":47,"ly":28,"mx":47,"my":27},...]';

Categories

Resources