I have a JSon like this :
[
{
"order_number": "",
"products": [
{
"line_id": "" ,
"cod_anf": "",
"static_field_2": "",
"nome_armazem": "",
"cod_armazem": "",
"gln": "",
"sku_ah": "",
"name": "",
"qty": "",
"_regular_price": "",
"_sale_price": "",
"line_total": "",
"desconto": "",
"pva": "",
"category": "",
"localidade": " ",
"codigo_ah": " ",
"Part of": ""
},
{
"line_id": "",
"cod_anf": "",
"static_field_2": "",
"nome_armazem": "",
}
],
{
"order_number": "",
"products": [
{
"line_id": 1,
"cod_anf": "",
"static_field_2": "",
I need to remove the "products" but keep it's content.
So the output should be like this:
Is it possible? I've tried and I cant make it work.
Thanks
You can use nested maps, spread operator, and flat function like below.
const json = [{
"order_number": "2",
"products": [{
"line_id": "",
"cod_anf": "",
"static_field_2": "",
"nome_armazem": "",
"cod_armazem": "",
"gln": "",
"sku_ah": "",
"name": "",
"qty": "",
"_regular_price": "",
"_sale_price": "",
"line_total": "",
"desconto": "",
"pva": "",
"category": "",
"localidade": " ",
"codigo_ah": " ",
"Part of": ""
},
{
"line_id": "",
"cod_anf": "",
"static_field_2": "",
"nome_armazem": "",
}
]
},
{
"order_number": "1",
"products": [{
"line_id": 1,
"cod_anf": "",
"static_field_2": "",
}
]
}
];
const flattened = json.map(r => [...r.products.map(q =>
({
order_number: r.order_number,
...q
})
)])
.flat(1);
console.log(flattened);
Try this, assign json to data
let obj = [];
data.forEach(order =>{
order.products.forEach(product => {
obj.push({
...product,
order_number: order.order_number
})
})
})
Related
I have a requirement where I need to partition incoming data to look like the following example:
//required payload
{
"campaign_id": "",
"recipient": {
"first_name": "",
"last_name": "",
"company": "",
"email": "",
"address_1": "",
"city": "",
"state": "",
"postal_code": "",
"identifier": ""
}
}
However, my issue is that the payload currently being posted to my endpoint looks like this:
//entire payload
"body": {
"info": [
{
"campaign_id": "",
"recipient": {
"first_name": "",
"last_name": "",
"company": "",
"email": "",
"address_1": "",
"city": "",
"state": "",
"postal_code": "",
"identifier": ""
}
}
],
"otherArray": [],
"otherString": "",
"otherString2": ""
}
}
What I want to do is extract the required data in my first example from the second example and then store it in a variable. Something like the below:
var requiredPayload = {"campaign_id": "", "recipient": {}};
What can I do to put campaign_id + recipient object in a single variable?
We should be able to assign the first element of the info array to the requiredPayload variable, like so:
const payload = {
"body": {
"info": [
{
"campaign_id": "",
"recipient": {
"first_name": "",
"last_name": "",
"company": "",
"email": "",
"address_1": "",
"city": "",
"state": "",
"postal_code": "",
"identifier": ""
}
}
],
"otherArray": [],
"otherString": "",
"otherString2": ""
}
};
var requiredPayload = payload.body.info[0];
console.log("requiredPayload:", requiredPayload);
try this:
var data={"body": {
"info": [
{
"campaign_id": "",
"recipient": {
"first_name": "",
"last_name": "",
"company": "",
"email": "",
"address_1": "",
"city": "",
"state": "",
"postal_code": "",
"identifier": ""
}
}
],
"otherArray": [],
"otherString": "",
"otherString2": ""
}
};
console.log(data.body.info[0]); //data is variable name
var info_data=data.body.info[0];
if info have multiple campaign_id then try this
$.each(data.body.info, function (indexInArray, valueOfElement) {
console.log(data.body.info[indexInArray]);
});
I have a flat array consisting of the following data
[{
"text": "",
"type": "true_false",
"ans": {
"ans_1": true,
"ans_2": false
},
"correct_ans": "",
"reason": "",
"passage": ""
},
{
"text": "",
"type": "true_false",
"ans": {
"ans_1": true,
"ans_2": false
},
"correct_ans": "",
"reason": "",
"passage": ""
}, {
"text": "",
"type": "passage",
"ans": {
"ans_1": "",
"ans_2": ""
},
"correct_ans": [],
"reason": "",
"passage": 3
}, {
"text": "",
"type": "multichoice",
"ans": {
"ans_1": "",
"ans_2": ""
},
"correct_ans": [],
"reason": "",
"passage": ""
}, {
"text": "",
"type": "passage",
"ans": {
"ans_1": "",
"ans_2": ""
},
"correct_ans": [],
"reason": "",
"passage": 3
}, {
"text": "",
"type": "passage",
"ans": {
"ans_1": "",
"ans_2": ""
},
"correct_ans": [],
"reason": "",
"passage": 5
}
]
But, I will like to group the array in the format below.
[{
"text": "",
"type": "true_false",
"ans": {
"ans_1": true,
"ans_2": false
},
"correct_ans": "",
"reason": "",
"passage": ""
},
{
"text": "",
"type": "true_false",
"ans": {
"ans_1": true,
"ans_2": false
},
"correct_ans": "",
"reason": "",
"passage": ""
}, {
"type": "passage",
"passage": 3,
"children": [{
"text": "",
"ans": {
"ans_1": "",
"ans_2": ""
},
"correct_ans": [],
"reason": ""
}, {
"text": "",
"ans": {
"ans_1": "",
"ans_2": ""
},
"correct_ans": [],
"reason": ""
}]
}, {
"text": "",
"type": "multichoice",
"ans": {
"ans_1": "",
"ans_2": ""
},
"correct_ans": [],
"reason": "",
"passage": ""
}, {
"passage": 5,
"type": "passage",
"children": [{
"text": "",
"ans": {
"ans_1": "",
"ans_2": ""
},
"correct_ans": [],
"reason": ""
}]
}
]
I have searched online but; couldn't get my desired result. I also cam across this Q/A Build tree array from flat array in javascript. I tried to tweak the answer but still couldn't get my desired result.
Based on the the object generated from this JSON data, how can I get only the z:row object values using javascript or jQuery? In other words, how can I filter object values from JSON?
{
"Response": {
"Request": {
"ConnectInfo": {
"USER_NAME": "",
"PASSWORD": "",
"PROJECT_ID": "",
"CONNECTION_NAME": "",
"APP_ID": "",
"CLIENT_IP": "",
"SITE": "",
"LANGUAGE": "",
"LANGUAGE_ID": "0"
},
"Method": {
"MethodID": "",
"Name": "",
"PARAMS": {
"PARAM": [{
"#id": "",
"#value": ""
},
{
"#id": "",
"#value": ""
},
{
"#id": "",
"#value": ""
},
{
"#id": "",
"#value": ""
}
]
},
"QueryControl": {
"chunk_num": "",
"rev_control": "",
"max_rows": ""
}
}
},
"MethodResponse": {
"Name": "",
"MethodID": "",
"DeResult": {
"xml": {
"#xmlns:dt": "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882",
"s:Schema": {
"#id": "RowsetSchema",
"s:ElementType": {
"#content": "eltOnly",
"#name": "row",
"s:AttributeType": [{
"#name": "BACKGROUND",
"#rs:maydefer": "true",
"#rs:nullable": "true",
"#rs:number": "1",
"#rs:writeunknown": "true",
"s:datatype": {
"#dt:maxLength": "2147483647",
"#dt:type": "string",
"#rs:precision": "0"
}
],
"s:extends": {
"#types": "rs:rowbase"
},
"rs:data": {
"z:row": [{
"#BACKGROUND": "",
"#DOC.APP_FORMAT_TYPE": "NO VALUE",
"#DOC.AUTHOR": "",
"#DOC.Access_UDF": "Public",
"#DOC.BOM_DEFINING": "Yes",
"#DOC.CAGE_CODE": "CAGE_CODE1",
},
{
"#BACKGROUND": "",
"#DOC.APP_FORMAT_TYPE": "NO VALUE",
"#DOC.AUTHOR": "",
"#DOC.Access_UDF": "Public",
"#DOC.BOM_DEFINING": "Yes",
"#DOC.CAGE_CODE": "CAGE_CODE1",
},
]
},
"ResultCode": "Ok",
"ExtraResults": "",
"Messages": "",
"Trace": ""
},
"ResultCode": "Ok",
"ExtraResults": "",
"Messages": ""
}
}
"
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
"{
"Response": {
"Request": {
"ConnectInfo": {
"LANGUAGE_ID": "0"
},
"Method": {
"MethodID": "",
"Name": "",
"PARAMS": {
"PARAM": [{
"#id": "",
"#value": ""
}]
},
"QueryControl": {
"chunk_num": "",
}
}
},
"MethodResponse": {
"Name": "",
"MethodID": "",
"DeResult": {
"xml": {
"#xmlns:dt": "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882",
"s:Schema": {
"#id": "RowsetSchema",
"s:ElementType": {
"#content": "eltOnly",
"#name": "row",
"s:AttributeType": [{
"#name": "BACKGROUND",
"#rs:maydefer": "true",
"#rs:nullable": "true",
"#rs:number": "1",
"#rs:writeunknown": "true",
"s:datatype": {
"#dt:maxLength": "2147483647",
"#dt:type": "string",
"#rs:precision": "0"
}
],
"s:extends": {
"#types": "rs:rowbase"
},
"rs:data": {
"z:row": [{
"#BACKGROUND": "",
"#DOC.APP_FORMAT_TYPE": "NO VALUE",
"#DOC.AUTHOR": "",
"#DOC.Access_UDF": "Public",
"#DOC.BOM_DEFINING": "Yes",
"#DOC.CAGE_CODE": "CAGE_CODE1",
},
{
"#BACKGROUND": "",
"#DOC.APP_FORMAT_TYPE": "NO VALUE",
"#DOC.AUTHOR": "",
"#DOC.Access_UDF": "Public",
"#DOC.BOM_DEFINING": "Yes",
"#DOC.CAGE_CODE": "CAGE_CODE1",
},
]
},
"ResultCode": "Ok",
"ExtraResults": "",
"Messages": ""
}
}
Thanks for help.
I found answer like below way we can pass through each object.
var obj = data;
var json = JSON.parse(data)
json.Response.MethodResponse.DeResult.xml["rs:data"]
I use jQuery get a JSON string from a API. I want to display it and change some field and PUT back to the API. But it seems to hard to gather information from input box and put it into right field.
Here is the json structure:
{
"economicStatus": "",
"title1": "",
"migrantEducation": "",
"disability": {
"status": "",
"section504Status": "",
"primaryDisability": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
},
"awaitingInitialIDEAEvaluation": ""
},
"giftedTalented": "",
"firstNationalEnrollment": "",
"alertMessageList": [
{
"message": "object",
"messageType": ""
}
],
"medicalAlertMessageList": [
{
"message": "",
"severity": ""
}
],
"projectedGraduationYear": "",
"onTimeGraduationYear": "",
"graduationDate": "",
"economicDisadvantage": "",
"otherIdList": [
{
"idValue": "object",
"idType": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
}
}
],
"name": {
"nameRole": "",
"actualNameOfRecord": {
"prefix": "",
"familyNameFirst": "",
"preferredFamilyNameFirst": "",
"givenName": "",
"middleName": "",
"familyName": "",
"preferredFamilyName": "",
"preferredGivenName": "",
"fullName": "",
"sortName": "",
"suffix": ""
}
},
"phoneNumberList": [
{
"number": "",
"extension": "",
"phoneNumberType": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
},
"listedStatus": ""
}
],
"emailList": [
{
"emailAddress": "",
"emailType": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
}
}
],
"localId": {
"idValue": "object",
"idType": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
}
},
"externalId": {
"idValue": "object",
"idType": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
}
},
"addressRefIdList": [
{
"value": "",
"addressRole": ""
}
],
"otherNameList": [
{
"nameRole": "",
"otherName": {
"prefix": "",
"familyNameFirst": "",
"preferredFamilyNameFirst": "",
"givenName": "",
"middleName": "",
"familyName": "",
"preferredFamilyName": "",
"preferredGivenName": "",
"fullName": "",
"sortName": "",
"suffix": ""
}
}
],
"demographics": {
"birthDateVerification": "",
"sexus": "",
"birthDate": "",
"subregionOfBirth": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
},
"stateProvinceOfBirth": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
},
"placeOfBirth": "",
"countryOfBirth": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
},
"countryArrivalDate": "",
"citizenshipStatus": "",
"languageProficiency": "",
"dwellingArrangement": "",
"maritalStatus": "",
"ethnicityList": [
{
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
}
],
"languageList": [
{
"languageCode": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
},
"languageType": "",
"dialect": ""
}
],
"countryOfResidency": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
},
"countryOfCitizenshipList": [
{
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
}
]
},
"electronicIdList": [
{
"idValue": "object",
"idType": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
}
}
],
"refId": ""
}
And here is a sample data I get back from API, with some field are null:
{
"disability": null,
"economicStatus": null,
"migrantEducation": null,
"title1": null,
"alertMessageList": null,
"medicalAlertMessageList": null,
"projectedGraduationYear": null,
"onTimeGraduationYear": null,
"graduationDate": null,
"giftedTalented": null,
"economicDisadvantage": null,
"firstNationalEnrollment": null,
"otherIdList": [
{
"idType": {
"code": "AuthenticationUsername",
"item": null,
"otherCodeList": null,
"codesetName": "HMH.OtherId"
},
"idValue": "ChK#username-60ty"
},
{
"idType": {
"code": "HMOF UserName",
"item": null,
"otherCodeList": null,
"codesetName": "HMH.OtherId"
},
"idValue": "ChK#username-60ty.arda.stu"
},
{
"idType": {
"code": "Role",
"item": null,
"otherCodeList": null,
"codesetName": "HMH.OtherId"
},
"idValue": "learner"
}
],
"addressRefIdList": null,
"name": null,
"localId": {
"idType": {
"code": "LASID",
"item": null,
"otherCodeList": null,
"codesetName": "HMH.LocalId"
},
"idValue": "79800003270ABCKrtg12"
},
"externalId": {
"idType": {
"code": "SASID",
"item": null,
"otherCodeList": null,
"codesetName": "HMH.ExternalId"
},
"idValue": "NULL"
},
"electronicIdList": null,
"otherNameList": null,
"demographics": null,
"phoneNumberList": null,
"emailList": null,
"refId": "3329962f-18ab-415f-addb-132cbe613d68"
}
Is there a simple way to change the fields in a page using input box? I try to create as many input boxes as needed and name them and associate them with corresponding JSON field... But it seems to tedious and can not update array dynamically.
I have a javascript db in the format
var db = [
{ "id": "500020", "type": "0", "address": "", "firstName": "bob", "lastName": "builder", "title": "Mr", "managerId": "0", "officeId": "222", "cellPhone": "", "officePhone": "001221212121", "email": "myemail#domain.com", "Hours": "", "Custface": "", "Hearloop": "", "Parking": "", "Toilets": "", "Wheelchair": "", "OfficeType": "", "lat": "", "lon": "", "Company": "mycompany","image": "1111" },
{ "id": "500025", "type": "0", "address": "", "firstName": "danny", "lastName": "mccanny", "title": "Mr", "managerId": "0", "officeId": "0", "cellPhone": "", "officePhone": "012545251", "email": "Danny#mccanny.com", "Hours": "", "Custface": "", "Hearloop": "", "Parking": "", "Toilets": "", "Wheelchair": "", "OfficeType": "", "lat": "", "lon": "", "Company": "dannycompany","image": "500025" }
]
I then reference it like this
this.employees = db;
in a function I then filter it when a user searches like this
var employees = this.employees.filter(function (element) {
var fullname = element.firstname + " " + element.lastname;
return fullname.tolowercase().indexof(searchkey.tolowercase()) > -1;
});
the problem is that it is searching every element of the object not just the firstName and the lastName.
Can someone tell me how to get it to filter on just the first and last name using either javascript or jquery
We can use jquery grep method to filter the data and the code will be
var employees = $.grep(this.employees, function(element){
var fullname = element.firstName + " " + element.lastName;
return fullname.toLowerCase().indexOf(searchkey.toLowerCase()) > -1;
})