Why does JSON.parse() add numbers in front of elements? [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 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.

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}]`))

How do I remove double quote "" from JSON Value [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 1 year ago.
Improve this question
I am doing my Javascript. And just imported JSON and printed it out with JSON.stringfy
I am using that site because the JSON contains the URL which I want to link.
I am not getting what I really need as the output.
description": "www.site.com"
This is what I get in my console:
"www.site.com"
This is what I actually want, and remove the quotes.
www.site.com
Please help!
Am not sure what your requirement is but you can do this.
Try Replace function;
const obj = {description: "www.site.com"};
JSON.stringify(obj.description).replace(/"/g,"");
var someStr = 'He said "Hello, my name is Foo"';
console.log(someStr.replace(/['"]+/g, ''));
That should do the trick... (if your goal is to replace all double quotes).

Remove First and Last double Quotes from "["Morning Shift","Day Shift"]" [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 years ago.
Improve this question
I want to remove First and Last Quotes from this array Using JavaScript:
"["Morning Shift","Day Shift"]"
After Remove quotes output look like this
["Morning Shift","Day Shift"]
You can try using JSON.parse():
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.
Demo:
var data = `["Morning Shift","Day Shift"]`;
data = JSON.parse(data);
console.log(data);

Converting JSON with leaves and nodes to HTML [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 3 years ago.
Improve this question
I have a string that looks like the one below – this is straight from the DB:
"{\"object\":\"value\",\"document\":{\"object\":\"document\",\"data\":{},\"nodes\":[{\"object\":\"block\",\"type\":\"paragraph\",\"data\":{},\"nodes\":[{\"object\":\"text\",\"leaves\":[{\"object\":\"leaf\",\"text\":\"here is some text\",\"marks\":[]}]}]}]}}"
This is the first time I see something like this. I can surely JSON.parse it but this leaves me with an object.
Is it a common thing to see somthing like this?
Is there a library for rendering this to HTML or do I have to write my own method to do this?
If the json structure is fixed, you can do something like this:
const json = JSON.parse("{\"object\":\"value\",\"document\":{\"object\":\"document\",\"data\":{},\"nodes\":[{\"object\":\"block\",\"type\":\"paragraph\",\"data\":{},\"nodes\":[{\"object\":\"text\",\"leaves\":[{\"object\":\"leaf\",\"text\":\"here is some text\",\"marks\":[]}]}]}]}}");
const leaves = [];
json.document.nodes.forEach(
n => n.nodes.forEach(
node => node.leaves.forEach(
leaf => leaves.push(leaf.text)
)
)
);
console.log(leaves);

How to transform a string of maps coordinates into an array javascript? [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 years ago.
Improve this question
I am drawing trails on a map and the coordinates of the trail are saved as string in a json file in the following format:
(43.886758784865066, 24.226741790771484),(43.90271630763887, 24.234981536865234)
I need to get these values and add them to an array:
coordinates= [43.886758784865066, 24.226741790771484,43.90271630763887, 24.234981536865234];
So how can I do this transition?
You can try that way
var string = '(43.886758784865066, 24.226741790771484),(43.90271630763887, 24.234981536865234)';
string.match(/\d+(\.\d+)/g).map(function(d){return d;});
You could just use regular expressions to parse those strings.
const match = string.match(/\((.*)\, (.*)\),\((.*)\, (.*)\)/)
/*
Matches
["(43.886758784865066, 24.226741790771484),(43.90271630763887, 24.234981536865234)", "43.886758784865066", "24.226741790771484", "43.90271630763887", "24.234981536865234"]
*/
const Array.prototype.slice.call(match).splice(1, 4)
/* Converts to array and takes the last three elements
["43.886758784865066", "24.226741790771484", "43.90271630763887", "24.234981536865234"]
*/

Categories

Resources