parse a string that contains array with strings - javascript

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));

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/

Get the array in a string [javascript]

I need to get the array in the string below:
"['Comedian', 'Actor']"
For example, for the above, I should get ['Comedian', 'Actor'].
Already working using eval(). Is there any other way to get the desired result?
Normally i would suggest using a JSON.parse functionality to do so, however since this is not a valid json format due to single quotes instead of double quotes you could try to first replace those and only then parse
const str = "['Comedian','Actor']";
console.log(JSON.parse(str.replace(/'/g, '"')));
or you could use JSON5
const str = "['Comedian','Actor']";
console.log(JSON5.parse(str));
<script src="https://unpkg.com/json5#^2.0.0/dist/index.min.js"></script>

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

get JSONS from a single string

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);

Categories

Resources