I keep getting this SyntaxError when reading json parameters from a rest api in javascript.
Any clue what it might be?
I appreciate any help
code:
parseJSONResponse : function(inResult) {
var jsonDoc = JSON.parse(inResult);
this.fmeForm = document.forms['fmeForm'];
ar parameters = jsonDoc.serviceResponse.parameters.parameter;
for( i = 0; i < parameters.length; i++) {
if(parameters[i].type === "LOOKUP_CHOICE") {
this.createComboBox(parameters);
}
if(parameters[i].type === "LISTBOX_ENCODED") {
this.createCheckboxGroup(parameters);
}
}
},
And the json from a rest look like this:
"serviceResponse": {
"parameters": {"parameter": [
{
"optionsType": "MULTICHOICE_CONFIG",
"type": "LISTBOX_ENCODED",
"options": {"option": [
{"value": "NOR"},
{"value": "NOR_roads"}
]}
},
{
"optionsType": "SINGLECHOICE_CONFIG",
"type": "LOOKUP_CHOICE",
"options": {"option": [
{
"displayAlias": "WGS84, Lat-Long; Degrees [LL84]",
"value": "EPSG:4326"
}
]}
},
{
"name": "GEOM",
"description": "",
"defaultValue": "<lt>?xml<space>version=<quote>1.0<quote><space>encoding= <quote>US_ASCII<quote><space>standalone=<quote>no<quote><space>?<gt><lt>geometry<gt> <lt>polygon<gt><lt>line<gt><lt>coord<space>x=<quote>-124<quote><space>y=<quote>48<quote><solidus><gt><lt>coord<space>x=<quote>-124<quote><space>y=<quote>49.399999999999999<quote><solidus><gt><lt><solidus>line<gt><lt><solidus>polygon<gt><lt><solidus>geometry<gt>",
"optionsType": "SINGLECHOICE_CONFIG",
"type": "GEOMETRY",
"options": {"option": {"value": "GEOM"}}
},
Thanx!
as everyone is stating, this is absolutely not a valid JSON, whatever the case, try validating your JSON in http://jsonlint.com/, this will show you exactly where your problem might be
That is either a excerpt from the JSON response or a very broken result. One problem is that the comma at the end makes it invalid JSON...
Related
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
I have this result JSON data for my api call, but when i try to access the data in the attribute "69106658_5" I cant, I am getting "Error: Uncaught SyntaxError: Invalid or unexpected token". I have a copy of what I am running on an online editoe below. I am guessing its because of the attribute contains an underscore.
let results=
{
"links": {
"data": {
"self": {
"body": "",
"content_type": "",
"href": "/api/v2/nodes/69107289/categories",
"method": "GET",
"name": ""
}
}
},
"results": [
{
"data": {
"categories": {
"58652374_10": [
"16",
"16.0.1",
"16.2",
"16.2.4"
],
"58652374_11": [
"English"
],
"58652374_12": [
"Windows"
],
"58652374_13": "2018-11-20T00:00:00",
"58652374_2": "Published",
"58652374_3": "19",
"58652374_4": "Video",
"58652374_5": "65",
"58652374_6": "How To",
"58652374_7": [
"basic"
],
"58652374_8": "237",
"58652374_9": "Content Server"
}
}
},
{
"data": {
"categories": {
"69106658_2": "You Tube",
"69106658_3": [
"End User"
],
"69106658_4": [
"69106508:7"
],
"69106658_5": "https://img.youtube.com/vi/j-aOeCpRvEs/hqdefault.jpg",
"69106658_6": false,
"69106658_7": "Engineering",
"69106658_8": null
}
}
}
]
}
var lookInto = results.results;
for( let key in lookInto ) {
var selectData = lookInto[key].data.categories;
console.log(selectData);
}
console.log( selectData.69106658_5 )
Attribute fields that begin with anything other than a letter (and some symbols like _), you have to use bracket notation to access.
Instead of selectData.69106658_5, try selectData['69106658_5']
The underscore shouldn't cause any problem.
If you want to access the property "69106658_5", you should do like this :
results.results[1].data.categories["69106658_5"]
I have a JSON response from my API that is structured like so:
{
"data": [
{
"id": "1", "name": "test"
},
{
"id": "2", "name": "test2"
}
]
}
When I reference data I get the array with each record. I need the curly braces to be brackets due to a plugin I am using requiring it to be an array.
Desired output:
[
["1", "test"],
["2", "test"]
]
How can I convert the above JSON to this?
Edit:
This turned out to be a problem with a plugin I was using, and I knew how to do this fine all along. Thought I was going crazy but my code was fine, some plugin was screwing things up.
You can do this using Array.prototype.map
var arr = json.data.map(function(x){
return [x.id, x.name];
});
Something like this maybe: http://jsfiddle.net/3gcg6Lbz/1/
var arr = new Array();
var obj = {
"data": [
{
"id": "1", "name": "test"
},
{
"id": "2", "name": "test2"
}
]
}
for(var i in obj.data) {
var thisArr = new Array();
thisArr.push(obj.data[i].id);
thisArr.push(obj.data[i].name);
arr.push(thisArr);
}
console.log(arr);
I wrote a quick script to parse two fairly large json file (~17k records) to do a comparison of the two. I have confirmed they are both valid json (via jsonlintpro) and the same format. (The source is the same so this should be a given. But, I always assume the mistake is mine. And I still do. Just somewhere else.) However, the parsed file just outputs [object, Object]. I'm wondering what the cause could possibly be?
The json format is like this small snippet (anonymized of course):
[
{
"id": "1234",
"name": "Name1",
"url": "https://localhost/Name1",
"date_created": "2013-07-05T18:47:05Z",
"date_cancelled": "",
"props": [
{
"id": "54321",
"type": "Client",
"value": "General Store"
},
{
"id": "65432",
"type": "Contact_Name",
"value": "Joe Smith"
}
]
},
{
"id": "23456",
"name": "Name2",
"url": "https://localhost/Name2",
"date_created": "2014-02-27T17:46:43Z",
"date_cancelled": "",
"props": [
{
"id": "34567",
"type": "Client",
"value": "Bait Shop"
}
]
}]
And here is the pertinent code:
var _ = require('underscore');
var recs = require('./prod.json');
printArr(recs);
console.log(recs.length);
function printArr(arr) {
arr.forEach(function(item) {
console.log(item + ", ");
});
}
Any guidance would be greatly appreciated.
UPDATE:
Ok, so apparently the issue is with my printArr function. I'm not sure what I'm doing wrong there. I'd like to figure it out because I want to expand upon that so I can print selectively.
the parsed file just outputs [object, Object].
This is the expected behavior BECAUSE you are concatenating an object with a string.
Try console.log(item) instead
console.log(item); should indeed print [object, Object], did you try to output its properties instead?
function printArr(arr) {
arr.forEach(function(item) {
console.log( item.id, item.name, item.url, item.date_created, item.date_cancelled, item.props, ';');
});
}
Just export the value from the prod.json file.
prod.json file
module.exports = [
{
"id": "1234",
"name": "Name1"
},
{
"id": "1234",
"name": "Name1"
}]
elsewhere
var recs = require('./prod.json')
console.log(recs)
Can anybody help me to get data from the json below.I have get a json data in the format below and in this json you can see that there is "{0}" in each record.So my question is how i can get data from this format or is there any way to remove "{0}" from the json.
[{
"ChkValue": "ChkValue",
"Description": "Description",
"Mode": "Mode"
}, {
"0": {
"ChkValue": "false",
"Description": "Made sure guards are in place on machine",
"Mode": "Eliminate"
}
}, {
"0": {
"ChkValue": "false",
"Description": "Use Liveguard at electrical source2",
"Mode": "Isolate"
}
}, {
"0": {
"ChkValue": "false",
"Description": "Wear ear-muffs when using machine",
"Mode": "Isolate"
}
}]
This is a basic javascript object traversal problem.
To access the data inside the second object (that says "Made sure guards are in place..."), you would do:
jsonObj[1]["0"].Description
You can use the JSON.parse() function to work with it in JS.
user JSON.parse() to iterate over JSON
FIDDLE
var a = '[{"ChkValue":"ChkValue","Description":"Description","Mode":"Mode"},{"0":{"ChkValue":"false","Description":"Made sure guards are in place on machine","Mode":"Eliminate"}},{"0":{"ChkValue":"false","Description":"Use Liveguard at electrical source2","Mode":"Isolate"}},{"0":{"ChkValue":"false","Description":"Wear ear-muffs when using machine","Mode":"Isolate"}}]';
var b = JSON.parse(a);
for(var i = 0; i < b.length; i++) {
if(typeof b[i]["0"] != "undefined") {
console.log(b[i]["0"].ChkValue);
console.log(b[i]["0"].Description);
console.log(b[i]["0"].Mode);
}
}
Use list[index][0]
var list = [
{
"ChkValue": "ChkValue",
"Description":"Description",
"Mode":"Mode"
},
{
"0": {
"ChkValue":"false",
"Description":"Made sure guards are in place on machine",
"Mode":"Eliminate"
}
},
{
"0": {
"ChkValue":"false",
"Description":"Use Liveguard at electrical source2",
"Mode":"Isolate"
}
},
{
"0": {
"ChkValue":"false","Description":"Wear ear-muffs when using machine",
"Mode":"Isolate"
}
}
];
console.log(list[1][0].ChkValue); // get "false"
I am getting a JSON in response from server:
{
"width": "765",
"height": "990",
"srcPath": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_MERGED_/1273.pdf",
"coverPage": "",
"documents": [
{
"index": "1",
"text": "Archiving Microsoft® Office SharePoint® Server 2007 Data with the Hitachi Content Archive Platform and Hitachi Data Discovery for Microsoft SharePoint",
"type": "doc",
"id": "HDS_054227~201106290029",
"children": [
{
"text": "Page 1",
"leaf": "true",
"pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_054227~201106290029/image_1.png"
},
{
"text": "Page 2",
"leaf": "true",
"pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_054227~201106290029/image_2.png"
}
]
},
{
"index": "11",
"text": "Brocade FCoE Enabling Server I/O Consolidation",
"type": "doc",
"id": "HDS_053732~201105261741",
"children": [
{
"text": "Page 1",
"leaf": "true",
"pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_053732~201105261741/image_1.png"
},
{
"text": "Page 2",
"leaf": "true",
"pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_053732~201105261741/image_2.png"
}
]
}
]
}
And I want to get pagelocation of the children.
Can anyone tell me how to do this?
Hi
i also want to get indexes from this and then want to get pagelocations of that particular children. Can you tell me how would i do that?
And also when i when i am getting indexes array it is returning me ,, only and not the index nos.
I am using following code for that :
indexes=response.documents.map(function(e){ return e.children.index; })
Thanks & Regards
If you're interested in simply retrieving all the page locations, you can do it using filter:
var locations = [];
json.documents.forEach(function(e,i) {
e.children.forEach(function(e2,i2) {
locations.push(e2.pageLocation);
)}
});
// returns flat array like [item1,item2,item3,item4]
You can get an array of arrays using map:
var locations = [];
var locations = json.documents.map(function(e) {
return e.children.map(function(e2) {
return e2.pageLocation;
});
});
// returns 2-dimensional array like [[item1,item2],[item1,item2]]
Your json response is an appropriate javascript object So you can access all elements of the object like you do as in back end.
here, you have an array of object of the type documents and each document object has array of objects of the type children. so
syntax would be
myjson.documents[0].children[0].pagelocation
( = http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_054227~201106290029/image_1.png)
will give you the very first page location..
and so on