How can I parse a stringified array of arrays (JavaScript)? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
I have a string which represents an array. The items in the array can be characters, or arrays. So a string might look like: [a,b,[[],[d]],[],[[[]]],[[c,[t,s]],b]]
And I want to parse that out so it's a proper array with subarrays etc
So "[a,b,[c,[d,e],[]]]" would become:
[ 'a',
'b',
[ 'c',
['d','e'],
[]
]
]
Is there an easy way to do this in JavaScript? Eg some kind of array equivalent of JSON.parse? I tried JSON.parse but it throws an error (not unexpectedly).

You would have to process it to convert it into a format that JSON.parse would be able to handle. Your test case is simple so it is possible with an easy regular expression.
const str = "[a,b,[c,[d,e],[]]]";
const obj = JSON.parse(str.replace(/([a-z]+)/gi,'"$1"'));
console.log(obj);

Related

How to parse object array that has been stringified using backslashes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
Improve this question
I have been searching for the correct regex for converting this string to JSON. It works if there is a single object in the array, but I believe the comma is messing it up somehow.
const json = "{\"userId\":44, \"userName\": \"Jim Coleman\"},{\"userId\":33515, \"userName\": \"Grace Mamaradlo\"}";
const obj = JSON.parse(json.replace(/("[^"]*"\s*:\s*)(\d{17,})/g, '$1"$2"'));
console.log(obj);
There was nothing wrong except that the list of objects was not contained within square brackets
const json = "{\"userId\":44, \"userName\": \"Jim Coleman\"},{\"userId\":33515, \"userName\": \"Grace Mamaradlo\"}";
console.log(JSON.parse(`[${json}]`))

Convert String Object to real Object (without quotes) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I'm having some trouble converting an object of type string to a real object.
Here is my example (it's a string):
{ id: true,translations:{id: true,text: true,language:{id: true,languageCode: true}},createdAt: true }
When I JSON.Stringify it I get:
"{ id: true,translations:{id: true,text: true,language:{id: true,languageCode: true}},createdAt: true }"
And when I JSON.Parse it I get the same result as the example above, I also did console log the type of it and I get it's of type string
Any Ideas on how I should convert it to a real object?
Thanks in advance
By far the easiest way to handle this is using the JSON5 library
const string="{ id: true,translations:{id: true,text: true,language:{id: true,languageCode: true}},createdAt: true }";
const object = JSON5.parse(string);
console.log(object)
<script src="https://unpkg.com/json5#2.2.1/dist/index.js"></script>

How to get data in object without looping [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to get data from the object without for loop
this is my object
data = [{name:"A",value:[java:45,c++:50]},{name:"B",value:[java:12,c++:47]},{name:"C",value:[java:15,c++:32]}]
Expected result:
result_name = ["A","B","C"]
result_java = [45,12,15]
The structure of your array data is invalid. You wrote nested array value in square brackets yet the items are written as key-value pairs.
There is no magical way you can obtain multiple data from an array without iteration/looping. (unless you already know the indices, but this defeats the purpose of using arrays)
That said, if you meant to avoid only the "for" loops, you can:
Fix your data as below.
let data = [
{name:"A", value:{java:45,"c++":50}},
{name:"B", value:{java:12,"c++":47}},
{name:"C", value:{java:15,"c++":32}}
];
Run an alternative loop such as below.
console.log(data.map(item => item.name)); // ["A", "B", "C"]
console.log(data.map(item => item.value.java)); // [45,12,15]

Why does JSON.parse() add numbers in front of elements? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a string where I build my build my "json-like" format, like
_toBeFormated =
[
{"foor":"bar","foo":"bar","foo":["bar,bar"]},
{"foor":"bar","foo":"bar","foo":["bar,bar"]},
{"foor":"bar","foo":"bar","foo":["bar,bar"]}
]
But after calling JSON.parse like _afterFormat = JSON.parse(_toBeFormated), my structure looks like the following:
_afterFormat =
0:{"foor":"bar","foo":"bar","foo":["bar,bar"]},
1:{"foor":"bar","foo":"bar","foo":["bar,bar"]},
2:{"foor":"bar","foo":"bar","foo":["bar,bar"]}
If I try to change to JSON Format at the beginning, like leaving out [ ], if failes to parse, also it looks like valid JSON to me. What am I missing, or why does it add the numbers at the beginning?
It doesn't add numbers. The data structure is an array. The tool you are using to look at the array is showing the index of each entry.

Convert Array to Json in angularjs not working [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
var test=["a","b","c"];
console.log(JSON.stringify(test));
I am stuck to doing this. i want output like this
{"a","b","c"}
But I am getting like this
["a","b","c"]
Can someone help me?
Both JavaScript and JSON can represent data using a dictionary-style (as in objects using {} characters) or an array-style (a list of objects with the [] characters).
With dictionary-style objects:
You use the {key1:value1, key2:value2} format
You always have key and value for any entry
{"a":0, "b":1} is valid
{"a", "b"} is not valid
With an array:
You use the ["a", "b", "c"] format
You only have values (and their position), no keys
So then, {"a", "b", "c"} is meaningless in both JavaScript and JSON.
You are defining an array in JavaScript:
var test=["a","b","c"];.
That is why the JSON.stringify(test) shows an array with [] and not an invalid syntax such as {"a","b","c"}.
You can't convert an Array to JSON object but what you can do is create an object, store in it the array and then convert it.
something like this:
var obj = {test: ["a", "b", "c"]};
console.log(JSON.stringify(obj));

Categories

Resources