So I have the following response:
{
"errors": [
{
"errorKey": "ERROR_NO_DELIVERY_OPTIONS",
"errorParameters": "[{\"errorMessage\":\"ERROR_DELIVERY_OPTIONS_YOU_SELECTED_NOT_AVAILABLE_NOW\",\"partNumbers\":[\"19308033\",\"19114798\"]},{\"errorMessage\":\"Pickup At Seller not available for these orderItemIds\",\"orderItemIds\":[\"10315031\",\"10315032\"],\"availableShipModeId\":\"13201\"}]",
"errorMessage": "ERROR_NO_DELIVERY_OPTIONS",
"errorCode": "ERROR_NO_DELIVERY_OPTIONS"
}
]
}
Unfortunately, I'm not sure how to work with the value of "errorParameters" since it just a string and not a simple key-value like the others. How would I extract all the information so I can work with it. A co-woker mentioned parsing it but not sure what he meant by that and how. Below is a more readable value. I'm working with javascript.
[
{
"errorMessage": "ERROR_DELIVERY_OPTIONS_YOU_SELECTED_NOT_AVAILABLE_NOW",
"partNumbers":
[
19308033,
19114798
]
},
{
"errorMessage": "No Shipmodes Available for these orderItemsIds",
"orderItemIds": [
10315031,
10315032
]
}
]
You will need to use JSON.parse to transform JSON strings into a JS Object.
You'll need to do this in your code. I.E.
data.errors.forEach(e => console.log(JSON.parse(e.errorParameters)))
You can use just JSON.parse() function, which changes string JSON representation into a JavaScript object.
const parsedErrorParameters = JSON.parse(data.errorParameters);
console.log(parsedErrorParameters[0].errorMessage); // ERROR_DELIVERY_OPTIONS_YOU_SELECTED_NOT_AVAILABLE_NOW
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
Related
How Can I get data from this json data ?
I get this data from the server, and it sends some database's datas by using sequelize.
Data what I want to access is
{result: "[{"userid":"a","speed":10},{"userid":"b","speed":20},{"userid":"c","speed":30}]"}]" }
And I tried console.log(result[0].userid);
But, I only got errer like this. ' Uncaught TypeError: Cannot read property 'userid' of undefined'.
Could you tell me what I have to fix?
You gave an invalid JSON, the json should be something like this:
Formatted JSON Data
{
"result":[
{
"userid":"a",
"speed":10
},
{
"userid":"b",
"speed":20
},
{
"userid":"c",
"speed":30
}
]
}
And after that you can access it:
const json = {
"result":[
{
"userid":"a",
"speed":10
},
{
"userid":"b",
"speed":20
},
{
"userid":"c",
"speed":30
}
]
};
console.log(json.result[0].userid);
Your JSON is badly formatted.
Try this instead...
{"result":[{"userid":"a","speed":10},{"userid":"b","speed":20},{"userid":"c","speed":30}]}
The main issues were...
{result: "[{"userid"
Should be...
{"result":[{"userid"
And
"speed":30}]"}]" }
Should be...
"speed":30}]}
Please note, the whitespace between the items makes no difference, but I have remove to reduce the size of the JSON string
I have a route used for AJAX calls. It gets items from a DB and returns a JSON array.
I'm using:
return reply({
myArray
}).code(200);
Everything works but my output in the browser is:
{
"myArray":[
{
"_id":"1",
"name":"Asd1"
},
{
"_id":"2",
"name":"Asd2"
}
}
But what I need is:
{
[
{
"_id":"1",
"name":"Asd1"
},
{
"_id":"2",
"name":"Asd2"
}
]
}
Very basically, I need to get rid the "myArray" parent element and leave just the array there. It looks like a simple task but I can't find documentation or samples anywhere.
Thanks,
Marco
This:
{
[
{
"_id":"1",
"name":"Asd1"
},
{
"_id":"2",
"name":"Asd2"
}
]
}
is invalid JSON notation. Within curly braces, you should have key-value pairs where keys are strings and values are valid JSON values (strings, numbers, booleans, null, arrays, or objects).
Perhaps what you expect is just the array:
[
{
"_id":"1",
"name":"Asd1"
},
{
"_id":"2",
"name":"Asd2"
}
]
which is valid JSON. In this case, you may simply send it to your reply function:
return reply(myArray).code(200);
For more info on JSON notation, see the article on MDN and play with JSON.stringify to develop better intuition on when the JSON you see is valid or not.
I'm trying to read a sub-array from a JSON file in Polymer in JS and return the sub-array to be used in a dom-repeat. However, it tells me that the sub-array is undefined. I tried re-structuring the JSON file in various ways but no luck. I think I'm not using the right syntax somewhere.
Right now the JSON looks like this:
{
"url": "dn8",
"volpage": "DN iii 1",
"languages": [{
"pt": {
"authors": ["Laera"],
"titlelan": "Title in Portuguese"
},
"fr": {
"authors": ["Moi"],
"titlelan": "Title in French"
},
"es": {
"authors": ["Jesus"]
}
}]
}
I'm trying to get a sub-array called languageData which just hold the specific data for an input-language. The input has the correct value for the inputLanguage, like for instance "pt". My JS looks like this:
Polymer({
is: 'test-data',
properties: {
inputUrl: String,
inputLanguage: String,
inputData: {
type: Array,
notify: true,
value: function(){return []}
},
languageData: {
type: Array,
computed: '_computeLanguage(inputData,inputLanguage)'
}
},
_computeLanguage: function(inputData,inputLanguage) {
var lanarray = inputData.languages[inputLanguage];
return lanarray ? lanarray : "";
}
});
Any help is very much appreciated!
As you can see the languages property of your JSON is not an Object but and Array.
Your "Polymer code" works well, the problem is that you are trying to get the languageData as if it were an Array:
var lanarray = inputData.languages[inputLanguage];
Actually, languages contains an array of objects and you can't find your lang object in this way.
A possible solution could be:
var lanarray = inputData.languages[0][inputLanguage];
Jsonlint display that this JSON object is valid:
[{"obj":{"markers":"[{\"k\":47.040182144806664,\"B\":0.52734375},{\"k\":50.90303283111257,\"B\":10.37109375},{\"k\":52.53627304145945,\"B\":-1.7578125},{\"k\":41.77131167976406,\"B\":-6.591796875}]","path":"[[47.040182144806664,0.52734375],[50.90303283111257,10.37109375],[52.53627304145945,-1.7578125],[41.77131167976406,-6.591796875]]"}}]
I'm trying to access to markers with the k, B and path elements but it's always set to undefined. Here is my code:
try {
var jsonData = JSON.parse(myJson);
console.log(jsonData.obj[0].markers[0].k);
}
catch (e) {
console.error("Parsing error:", e);
}
Can someone tell me how to access to the element of my JSON object properly? Thanks for the help.
Something must have gone wrong in creating this string. Yes, it's valid JSON, but it has a different format than you think, because you escape control characters like " and [, ].
Try this string instead:
[
{
"obj":{
"markers":[
{
"k":47.040182144806664,
"B":0.52734375
},
{
"k":50.90303283111257,
"B":10.37109375
},
{
"k":52.53627304145945,
"B":-1.7578125
},
{
"k":41.77131167976406,
"B":-6.591796875
}
],
"path":[
[
47.040182144806664,
0.52734375
],
[
50.90303283111257,
10.37109375
],
[
52.53627304145945,
-1.7578125
],
[
41.77131167976406,
-6.591796875
]
]
}
}
]
as opposed to your string:
[
{
"obj":{
"markers":"[{\"k\":47.040182144806664,\"B\":0.52734375},{\"k\":50.90303283111257,\"B\":10.37109375},{\"k\":52.53627304145945,\"B\":-1.7578125},{\"k\":41.77131167976406,\"B\":-6.591796875}]",
"path":"[[47.040182144806664,0.52734375],[50.90303283111257,10.37109375],[52.53627304145945,-1.7578125],[41.77131167976406,-6.591796875]]"
}
}
]
You have two issues from what I can tell:
One, yes it is valid Json but the marker and path object values are enclosed in string quotes:
"markers":"[{\"k\":47.040182144806664,\"B\":0.52734375},{\"k\":50.90303283111257,\"B\":10.37109375},{\"k\":52.53627304145945,\"B\":-1.7578125},{\"k\":41.77131167976406,\"B\":-6.591796875}]",
"path":"[[47.040182144806664,0.52734375],[50.90303283111257,10.37109375],[52.53627304145945,-1.7578125],[41.77131167976406,-6.591796875]]"
what you rather want is:
"markers":[{"k":47.040182144806664,"B":0.52734375},{"k":50.90303283111257,"B":10.37109375},{"k":52.53627304145945,"B":-1.7578125},{"k":41.77131167976406,"B":-6.591796875}],
"path":[[47.040182144806664,0.52734375],[50.90303283111257,10.37109375],[52.53627304145945,-1.7578125],[41.77131167976406,-6.591796875]]
But given the above, also not escape the k and b object names for markers \"k\" should be "k":
So the completed edited JSON would look like this:
[{"obj":{"markers":[{"k":47.040182144806664,"B":0.52734375},{"k":50.90303283111257,"B":10.37109375},{"k":52.53627304145945,"B":-1.7578125},{"k":41.77131167976406,"B":-6.591796875}],"path":[[47.040182144806664,0.52734375],[50.90303283111257,10.37109375],[52.53627304145945,-1.7578125],[41.77131167976406,-6.591796875]]}}]
I have a JSON object that looks like this:
var json = {
"cj-api": {
"products": [
{
"$": {
"total-matched": "231746",
"records-returned": "999",
"page-number": "1"
},
"product": [ {... // contains lots objects with the data I'd like to access } ]
As noted above, I want to access the product array of objects. I can't seem to do this though. I've tried:
console.log(json['cj-api']['products'][0]['product']);
But I get typeError: Cannot read property 'products' of undefined.
What's the correct way to access the array of product (note, singular product, not products). This data is coming from an external source so I can't alter the hyphen in cj-api.
EDIT: Here's what the raw console log of json looks like:
{"cj-api":{"products":[{"$":{"total-matched":"231746","records-returned":"999","page-number":"1"},"product":[{ << lots of data in here>>
EDIT 2: To further clarify, I got this object by running JSON.stringify(result) after I put some XML into XML2js.
i have tried the following JSON structure:
var json = {
"cj-api": {
"products": [
{
"$": {
"total-matched": "231746",
"records-returned": "999",
"page-number": "1"
},
"product": [
{
"a": "a",
"b": "b",
"c": "c"
}
]
}
]
}
};
with the log statement as:
console.log(json['cj-api']['products'][0]['product']);
And result is as follows:
[Object { a="a", b="b", c="c"}]
Well your way of accessing json is absolutely correct. This is for debugging. Try
console.log(json['cj-api']);
console.log(json['cj-api']['products']);
console.log(json['cj-api']['products'][0]);
console.log(json['cj-api']['products'][0]['product']);
Which ever line returns undefined means your json is broken there.
If this doesn't work then you need to check for other similar keys. Maybe they value you are trying to find is actually undefined.
Maybe you are trying to loop. If you are then check for the condition if (JSONStructure[key]==undefined) console.log("Undefined at position ..."). That is the only way if you have valid JSON.
typeError: Cannot read property 'products' of undefined means that json exists, but json['cj-api'] is undefined. If you are sure that you are using the right variable name, I think this might be a scope issue, where you are using an other variable than you intend to. json might be the json string, instead of the array-like object. Try renaming your variable and see if you still get this problem. Otherwise the string is not automatically parsed for you and you'll have to parse it with JSON.parse( ... ).
Edit:
var json = '{ "me": "be an evil string" }';
console.log( json ); //'{ "me": "be an evil string" }'
console.log( json['me'] ); //undefined
console.log( JSON.parse( json )['me'] ); // 'be an evil string'
Since your question is missing the last
}
]
}
}
and others here changed your example and made it work, did you try to correct it?
If not then I suggest you or the dataprovider correct the structure of the reply.
I have tried below json structure
var json={
"cj-api": {
"products": [
{
"$": {
"total-matched": "231746",
"records-returned": "999",
"page-number": "1"
},
"product": []
}
]
}
}
now json['cj-api']['products'][0]['product'] will work