I'm fetching product details using an API which gives the response as a JSON object.
{
"productBaseInfo": {
"productIdentifier": {
"productId": "EKTDDD23232zYHR94E4",
},
"productAttributes": {
"title": "Nova KT 72BC 1 Electric Kettle",
"imageUrls": {
"400x400": "http://img5a.flixcart.com/image/electric-kettle/4/e/4/nova-kt-722-c-kt-722c-400x400-imadddh2fdvuzpxz.jpeg",
"75x75": "http://img6a.flixcart.com/image/electric-kettle/4/e/4/nova-kt-722-c-kt-722c-75x75-imadddh2fdvuzpxz.jpeg",
},
"sellingPrice": {
"amount": 599.0,
"currency": "INR"
},
"productUrl": "http://dl.mykart.com/dl/nova-kt-722c-1-electric-kettle/p/itmdddf398rhhhz2?pid=EKTDDDEGXYHR94E4&affid=userid"
}
}
}
Now I want to get the productId, title in ProductAttributes, and all the image urls and productURL.
I tried
var productURL = JSON["productAttributes"].productUrl
But it returns an error productUrl not found error. Looking for suggestion on how to extract the data. Thanks in advance.
productAttributes is inside productBaseInfo. So you need to access it like this
console.log(JSON.productBaseInfo.productAttributes.productUrl);
// http://dl.mykart.com/dl/nova-kt-722c-1-electric-kettle/p/itmdddf398rhhhz2?pid=EKTDDDEGXYHR94E4&affid=nikhilgeo
Related
I need help in my one issue. I have write the program in that I am use map in node.js.
I am testing this program using postman by sending JSON structure, however I am not get specific value in console which I am printing.
Please see below code .
async CreateProduceMVPRateAsset(data, callback) {
// Create a new file system based wallet for managing identities.
var ProducePRICE = {};
var MVPRATE = new Map();
var MVPPRICE =[];
var MVPPRICE_BS ={};
var MVPPRICE_LB ={};
var PRODUCENAME = data.PRODUCE
console.log('PRODUCENAME', PRODUCENAME);
var COUNTRY = data.COUNTRY;
console.log('COUNTRY', COUNTRY);
var STATE = data.STATE;
console.log('STATE', STATE);
MVPRATES = data.MVPRATES;
console.log('MVPRATERATE', MVPRATES); // not getting value of MVPRATES from request body
}
JSON structure which is sending using POSTMAN
{
"username": "admin2",
"PRODUCE": "Apple",
"STATE": "MI",
"COUNTRY": "US",
"MVPRATES": {
"fuji": {
"VARIETY": "fuji",
"RATE": [
{
"UNIT": "Bussel",
"CURRENCY": "USD",
"VALUE": 10.25,
"UIDISPLAY": true
}
]
},
"gala": {
"VARIETY": "gala",
"RATE": [
{
"UNIT": "Bussel",
"CURRENCY": "USD",
"VALUE": 10.25,
"UIDISPLAY": true
}
]
}
}
}
output
Any help very appreciable
Thanks
Abhijeet
That's how logs will show up for the non-primitive type of data. Try stringifying the response like:
MVPRATES = data.MVPRATES;
console.log('MVPRATERATE', JSON.stringify(MVPRATES));
This will help you in printing actual values to the logs. A better approach will be to use a logging module like winston and configure all such things and many more.
Sorry to waste you all time I think I miss the var in front of MVPRATES. It should be
var MVPRATES = data.MVPRATES;
I want to add a new object for each nested array. I'm calling this function any time I add a product to my orderintake:
add2order(productID, productName, productRatePlans) {
this.orderIntake.push({ productID, productName, productRatePlans });
let i = 0;
this.orderIntake[0].productRatePlans[0].productRatePlanCharges.forEach(element => {
i++;
this.orderIntake[0].productRatePlans[0].productRatePlanCharges[
i
].quantity = this.orderIntake[0].productRatePlans[0].productRatePlanCharges[
i
].defaultQuantity;
});
}
this is an example response from the server:
{
"id": "8adc8f996928b9a4016929c59b943a8f",
"sku": "SKU-00006778",
"Partner_Account_ID__c": null,
"productRatePlans": [
{
"id": "8adce4216928c28d016929c59bff3372",
"status": "Active",
"name": "Enterprise",
"description": null,
"effectiveStartDate": "2016-02-26",
"effectiveEndDate": "2029-02-26",
"productRatePlanCharges": [
{
"id": "8adc8f996928b9a4016929c59d183a92",
"name": "USAGE_COUNTER_2",
"type": "Usage",
"model": "Volume",
"uom": "Each",
"pricingSummary": [
"Up to 5000 Each: USD0 flat fee"
],
"pricing": [
{
...
}
],
"defaultQuantity": null,
"applyDiscountTo": null,
"discountLevel": null,
"discountClass": null,
...
"financeInformation": {
..,
}
}
]
}
],
"productFeatures": [
{
...
}
]
}
The data is being retrived this way from an external REST backend so unfortunately I can't initialize the data including the new property...
so in every productRatePlanCharges there should be 1 new object 'quantity'.
How can I add this field to every productRatePlanCharges?
Right now I'm getting: ERROR
TypeError: Cannot read property 'productRatePlanCharges' of undefined
And how can I make sure I'm always adding this to the last orderIntake element? Don't mind productRatePlans there is only 1 in each orderintake...
thanks for your support!
Here you have to create productDetails object with inititalised array like below so that you won't get the error.
add2order(productID, productName, productRatePlans) {
// Create object like below
let productDetails = { productID : productID, productName : productName, productRatePlans : productRatePlans
}
this.orderIntake.push(productDetails);
let i = 0;
this.orderIntake[0].productRatePlans[0].productRatePlanCharges.forEach(element => {
i++;
this.orderIntake[0].productRatePlans[0].productRatePlanCharges[
i
].quantity = this.orderIntake[0].productRatePlans[0].productRatePlanCharges[
i
].defaultQuantity;
});
}
Hope this will help!
as you used Angular you probably use Typescript too. I recommend that you create a model like your incoming model and there define your quantity: number inside productRatePlanCharges object. then map the incoming data to your own model. therefore you will have a quantity=0 in your model that you can change it later in a loop.
If you want to continue with your own way take a look at this:
Add new attribute (element) to JSON object using JavaScript
there is no problem to add an element to current model almost like you did, and the problem might be somewhere else as your error refers to existence of productRatePlanCharges!
as you used forEach I prefer to use that 'element' and double iterating with i++; is not a good idea to me.
this might be better:
element.quantity = element.defaultQuantity;
This question is purely syntax. I'm trying to insert a generated JSON object into another JSON object.
Let's say my JSON looks like this:
var database =
{
"john": {
"pwd": "somehashrighthere",
"mail": "example#gmail.com"
}
}
This object stores a hash and an email under the users name. Now I want to insert a new user:
var name = "somename";
var pwd = "hash";
var mail = "email#email.net";
If I try to insert this into a json object as by
database.name =
{
"pwd": pwd,
"mail": mail
}
I would expect an output that fills the gaps:
{
"john": {
"pwd": "r1pper",
"mail": "example#gmail.com"
},
"somenamerighthere": {
"pwd": "hash",
"mail": "email#email.net"
}
}
Instead the javascipt takes the expression quite literall:
{
"john": {
"pwd": "r1pper",
"mail": "example#gmail.com"
},
"name": {
"pwd": "pwd",
"mail": "mail"
}
}
I'm quite new to javascript/json and would appreciate if you guys exlain to me how one could dynamically(!) generate json objects and feed them into a bigger data structure. None of the answers I found on SO could solve this problem in a way I could understand. Do I need to make changes to the datastructure, or have I just misunderstood the javascript/node.js syntax. Thanks in advance :)
Edit: I solved the problem quite simply. Turns out javascript actually passes the variables into the json and I was just confused:
{
"john": {
"pwd": "r1pper",
"mail": "example#gmail.com"
},
"name": {
"pwd": "hash",
"mail": "email#email.net"
}
}
Now we just need to pass the name dynamically, which can be solved by treating the JSON, as if it was an array:
database[name]
treats name as a variable.
Edit 2:
The comments below came in while editing. I apologize for that.
you should use database[name] (take the value of name and use as index)
instead of database.name (use the string 'name' as index)
I am trying to query some data from a contentful API using gatsby's built-in graphiQL.
EDIT: after a suggestion from the comments, I made a introspection query to get the schema information:
{
"name": "contentfulStranNaslovQueryString_2",
"kind": "INPUT_OBJECT"
}
When I run this query:
{
contentfulStran {
id
naslov
}
}
I get the expected result (the first entry for the data model):
{
"data": {
"contentfulStran": {
"id": "c2tD44y2tDe8QC4yqkwMOgo",
"naslov": "Novice"
}
}
}
But now I would like to pass in a query parameter that only gets data specified on the naslov field. I tried this:
{
contentfulStran(naslov: "Ponudba") {
id
naslov
}
}
But I am getting the following error:
{
"errors": [
{
"message": "Argument \"naslov\" has invalid value \"Ponudba\".\nExpected \"contentfulStranNaslovQueryString_2\", found not an object.",
"locations": [
{
"line": 2,
"column": 27
}
]
}
]
}
What am I doing wrong?
Miha answered his own question in the comments. The correct way to filter is:
{
contentfulStran(naslov: {eq: "Ponudba"})
{
id
naslov
zaporedje
tekst
{
tekst
}
}
}
Note the {eq: "param"} object instead of just giving the param.
I'm trying to access the Google Classroom API with Javascript and am running into a syntax problem when trying to create a student.
The relevant code is
function createStudent () {
var course_id = '146694xxx'
var enrollment_code = '7ytxxx'
var student = {
userId: 'xxx#gmail.com'
}
student = gapi.client.classroom.courses.students.create({courseId:course_id,enrollmentCode:enrollment_code, params: student}).execute();
}
The problem is with the named parameter for the student object. It is where I have "params". I have tried every name I can think of like requestBody, body and a million others. The error I get is
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"params\" at 'student': Cannot find field.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "student",
"description": "Invalid JSON payload received. Unknown name \"params\" at 'student': Cannot find field."
}
]
}
]
}
}
How should I be representing that request body object in the call?
Thanks.
Harry