JSON.parse fails [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to parse a JSON string. It says 'Unexpected token ] in JSON at position 361'. I have the feeling I just overlooked something silly.
{
"username": "bob",
"user_found": "time",
"meta": [
{
"name" : [
{
"hash" : [
{
"content" : "con1",
"cont2" : "con2",
"date" : "time"
},
],
},
],
},
],
}

Figured it out, commas are invalid. Even for an expanding mono structure.

The reason is because of the unwanted comma at the end of each of the object and array.

The error message is saying it does not expect the bracket due to the commas saying there's going to be another item in the objects or arrays.
Remove the commas at the end of the list items:
{
"username": "bob",
"user_found": "time",
"meta": [
{
"name" : [
{
"hash" : [
{
"content" : "con1",
"cont2" : "con2",
"date" : "time"
}
]
}
]
}
]
}
Try entering it in a json validator to check that it's valid, e.g.: https://jsonlint.com/

Related

Cannot Access JSON array elements [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
I seem to be having trouble accessing individual elements of a json array. Given this code:
json_array=JSON.parse(data);
console.log(json_array);
console.log(json_array.title);
console.log(json_array.requester);
console.log(json_array.none);
This is the output:
Array(3)
0: {requester: false}
1: {title: false}
2: {none: true}
length: 3
[[Prototype]]: Array(0)
undefined
undefined
undefined
Obviously I am somehow accessing the individual elements incorrectly. What am I missing? TIA.
It seems like you forget the index when accessing array
console.log(json_array[0].title);
console.log(json_array[1].requester);
console.log(json_array[2].none);
You need to access json_array this way:
console.log(json_array);
console.log(json_array[0].title);
console.log(json_array[1].requester);
console.log(json_array[2].none);
This is because your json_array is an array with three objects, like this:
[
{
"title": ""
},
{
"requester": ""
},
"none": ""
]
An alternative solution would be to destructure your array into individual variables,which you can use accordingly :
const arr = [
{title:"title"},
{requester:"requester"},
{none:"none"}
]
const [title,requester,none] = arr
console.log(title,requester,none)
Please try this:
json_array = Object.assign([], json_array).reduce((a, b)
=> Object.assign(a, b), {});
console.log(json_array);
console.log(json_array.title);
console.log(json_array.requester);
console.log(json_array.none);

What is the parenthesis in a json? [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
An API call returns a json looking like below:
{
"applicationData": {
"outerMap": {
"record(id=1, description=description)": {
"key(id=1, englishDescription=enDescription)": [
{
"id": 1,
"description": "point 1"
},
{
"id": 2,
"description": "point 2"
}
]
}
}
},
"messages": [
"successfully"
],
"httpStatus": "OK"
}
The return type of the API call is:
public class Data {
private Map<Record, Map<Key, List<Point>>> outerMap;
}
I don't remember parenthesis is used in json, so quite confused about it. Seesm it could be used to "de-serialize" to restore the object?
Every valid string can be a valid key in JSON. So parentheses in key names are possible.
It is up to the application to interpret the content of the parentheses.

Best practice for accessing information within an array of objects. [.find() vs. .filter() vs. creating a reference object] [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Does anybody know if it's more efficient to create a new helper object or use locator methods [like .find() or .filter()] to help translate some unique identifiers in a set of data to the names they represent?
I'm working with an array of objects to make a table; each object holds the information to display on one row of the table.
example:
// Data:
[ { id: someCardIdxx,
name: "card1",
pos: 1,
customFields:
[{ id: "1234",
value: 32 },
{ id: "4321",
idValue: "876"}],
},
{ //...x50
}
]
To get information about the customFields, I need to refer to an kind of definition object that applies to the entire list. It has data for understanding how to translate the custom field id's and idValue's.
example:
// Custom Field Definitions:
[ { id: "1234",
type: "number",
display: true,
name: "Size",
},
{ id: "4321",
type: "list",
display: true,
name: "Color",
options:
[{ id: "987",
name: "Yellow"},
{ id: "876",
name: "Green"}]
},
{ // ...x8
}
]
When analyzing each of the card objects in the Data array, I can use the Definitions array with the help of .find() or .filter(), but that means searching for an object within the Definitions array each time I want to interpret a card.
Would it be better for optimization if I took the Definitions array of customField objects and converted it into a single organized object that I could reference?
The end goal could be something like this, with the field ids used as property keys:
// New Definitions Object:
{ "1234": "Size",
"4321": {
"name": "Color",
"options": {
"987": "Yellow",
"876": "Green"
}
},
// ...x8
}
Is it faster to access this information as properties after creating an object or by finding it in the array with the appropriate id? Which of these strategies (or other) would typically be best to use?
I am not sure is it about best practices or just my experience. From my point of view you should not build your application data model looking to data provider (backend) responses structure, but looking to your functionality.
According to my experience you should keep collection of items in array only if items order is important. If each item has unique identifier and order does not meter the best option is regular Map (or id to value object)
But any way, for 50 items you can do it in any way, the difference will be invisible

AngularJS - How create a form with specific format? [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 7 years ago.
Improve this question
I'm newbie in AngularJS and I'm working on ACTIVITI.
I need to create a form in a specific format. Every response must be in the form of:
{
"taskId" : "5",
"properties" : [
{
"id" : "room",
"value" : "normal"
}
]
}
The user input some text in the form, and the attribute "value" need to be equal to the user input.
How to reach this in AngularJS?
you bind your input with the ng-model directive to your value:
controller:
$scope.toto = {
"taskId" : "5",
"properties" : [
{
"id" : "room",
"value" : "normal"
}
]
}
.html:
<input ng-model="toto.properties[0].value">
I think you want this:
<input ng-model="formInfo.taskId">
<input ng-model="formInfo.properties.id">
<input ng-model="formInfo.properties.value">
controller:
console.log($scope.formInfo.taskId);
console.log($scope.formInfo.properties.id);
console.log($scope.formInfo.properties.value);

Arrays inside objects [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 8 years ago.
Improve this question
I'm trying to create an object that inside him have some fields, one of them supposed to be an array.
var ob=
{
name : "asdad"
array ?
}
But I dont know how to.
Can anyone help me?
Thank you!
a value is just a value, whether it be a simple scalar or a complicated looking object.
var obj = {
name: "user name",
stocks: ["AAPL", "GOOG"],
cars: [ {make:"Honda", model: "accord"},
{make:"BMW", model: "525", year: 2014}
]
};
do something like this:
var ob= {
name : "asdad",
myArray : ["value1", "value2", "etc."]
}
It would be helpful for you to go over this MDN doc.

Categories