How to know fields name of array by JSON code - json

I have a code :
var ListeSortie =
[
{
"Champ1": "Texte 1",
"Champ2": "Texte 2",
"Champ3": "Texte 3"
},
{
"Champ4": "Texte 4",
"Champ5": "Texte 5",
"Champ6": "Texte 6"
}
]
console.log('_______Liste 1_____________')
console.log(ListeSortie[0])
console.log('_______Liste 2_____________')
console.log(ListeSortie[1])
the Output :
_______Liste 1_____________
{Champ1: "Texte 1", Champ2: "Texte 2", Champ3: "Texte 3"}
_______Liste 2_____________
{Champ4: "Texte 4", Champ5: "Texte 5", Champ6: "Texte 6"}
My question : How can i do to have juste the value Name of my objet like this:
Champ1
Champ2
Champ3
Champ4
Champ5
Champ6
Many thanks in advance

You can loop through your array and use Object.keys method to get the keys.
https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Here's the answer:
First we loop through the ListeSortie to get the list of object in the ListeSortie.
Then we use the method Object.keys to get the key from the object inside ListeSortie.
Lastly, we for each every keys array and console.log the value.
ListeSortie.forEach(x => Object.keys(x).forEach(y => console.log(y)))

Related

Is there a way where all the values from a JSON column can be used as query params to fetch values in db?

I have a lookup table with columns old_item_code, description, new_item_code.
Now my items is an array of objects.
"items":[
{
"code": "OLDCODE1",
"description": "sample description1",
"value": "Sample value1"
},
{
"code": "OLDCODE2",
"description": "Sample Description2",
"value": "Sample Value 2"
}
]
Now I want my items array to be replaced with new item code which has to be queried from the lookup table(postgres).
I want my final result to be like
"items": [
{
"NEWCODE1": "Sample Value 1",
"NewCODE2": "Sample Value 2"
}
]
In PSQL, using where with array will get you the new code. Use a map to get its values.

Get array of JSON keys in react native

I have a JSON data in the format
[{
"Heading1": [{
"questionID": "q1",
"questionTitle": "Question 1",
"question": "This is question1",
"status": 0,
"files": [],
"uploadType": "none"
}, {
"questionID": "q2",
"questionTitle": "Question 2",
"question": "This is question2",
"status": 0,
"files": [],
"uploadType": "none"
}]
}, {
"Heading2": [{
"questionID": "q3",
"questionTitle": "Question 11",
"question": "This is a question11",
"status": 0,
"files": [],
"uploadType": "none"
}]
}, {
"Heading3": [{
"questionID": "q4",
"questionTitle": "Question 1",
"question": "This is a question",
"status": 0,
"files": [],
"uploadType": "none"
}]
}]
I'm trying to get all the titles in a format like [{"Title":"Heading1"},{"Title":"Heading2"},{"Title":"Heading3"}]
How should I go about it?
First, if you really have JSON (e.g., your starting point is a string, such as from an ajax response), you parse it via JSON.parse to get an array of objects. Then you loop the array and get the only key from each of those top-level objects via Object.keys(x)[0] and map that to an array of objects in the form you want:
const json = '[{"Heading1":[{"questionID":"q1","questionTitle":"Question 1","question":"This is question1","status":0,"files":[],"uploadType":"none"},{"questionID":"q2","questionTitle":"Question 2","question":"This is question2","status":0,"files":[],"uploadType":"none"}]},{"Heading2":[{"questionID":"q3","questionTitle":"Question 11","question":"This is a question11","status":0,"files":[],"uploadType":"none"}]},{"Heading3":[{"questionID":"q4","questionTitle":"Question 1","question":"This is a question","status":0,"files":[],"uploadType":"none"}]}]';
const parsed = JSON.parse(json);
const result = parsed.map(entry => {
return {Title: Object.keys(entry)[0]};
});
console.log(result);
The map callback can be a concise arrow function, but I thought using the verbose form above would be clearer. The concise form would be:
const result = parsed.map(entry => ({Title: Object.keys(entry)[0]}));
Note that using Object.keys in this way is only reliable if the objects really have just one property, as in your question. If they have more than one property, the order the properties are listed in the array from Object.keys is not defined (even in ES2015+, where properties do have an order — Object.keys is not required to follow that order [although it does on every modern engine I've tested]).

How to create nested object in MongoDB schema, nested object and array MEAN stack

I'm trying to design a nested MongoDB schema.
Currently, I have this schema and it's working:
var CompanySchema = new mongoose.Schema({
name: String,
rate: number
date: Date
})
But I wanna expend it to get:
var CompanySchema = new mongoose.Schema({
name: String,
currency: {
mxn: Number,
php: Number,
},
source: [String],
deliveryMethod: [String],
date: Date
})
For source, I want to get an array of inputs ex. ["bank", "debit card", "agent"]
and almost samething for deliverymethod.
But either my input is wrong or my schema, because the value for source saves as one long string, not a separated value.
Also, I think the way I designed the currency to have more currency rate is correct but I don't know how my input json should suppose to be.
I tried it in postman:
{
"name": "google",
"currency": {
"mxn": 20,
"php": 30
}
}
and this is the result i got:
{
"status": 201,
"data": {
"__v": 0,
"name": "google",
"date": "2017-12-06T22:38:45.896Z",
"_id": "5a2871752e3b7343dc388549",
"deliveryMethod": [
null
],
"source": [
null
]
},
"message": "Succesfully Created Company"
}
1- if my currency nested schema is correct how should be my post json file be?
2- how can I get source and deliveryMethod as an array of string?
The JSON in the request body should look like this:
{
"name": "google",
"currency": {
"mxn": 20,
"php": 30
},
"source": ["source1", "source 2", "source 3"],
"deliveryMethod": ["delMetd 1", "delMetd 2", "delMetd 3"],
"date": "2015-11-27T23:00:00Z"
}
I copy/pasted your code and tried with Postman. The response I got back was:
{
"__v": 0,
"name": "google",
"date": "2015-11-27T23:00:00.000Z",
"_id": "5a2915295c5f714f7cb25d90",
"deliveryMethod": [
"delMetd 1",
"delMetd 2",
"delMetd 3"
],
"source": [
"source1",
"source 2",
"source 3"
],
"currency": {
"mxn": 20,
"php": 30
}
}
If I connect to the database with the mongo shell and run db.companies.find().pretty() I get this result:
{
"_id" : ObjectId("5a2915295c5f714f7cb25d90"),
"name" : "google",
"date" : ISODate("2015-11-27T23:00:00Z"),
"deliveryMethod" : [
"delMetd 1",
"delMetd 2",
"delMetd 3"
],
"source" : [
"source1",
"source 2",
"source 3"
],
"currency" : {
"mxn" : 20,
"php" : 30
},
"__v" : 0
}
Your schema is fine. You can try dropping the collection (db.companies.drop()) if you can't get it to work. Start with a fresh one if you don't have any important data in it.

DocuSign: How to add recipientSignatureProviderInfo to the payload text/csv of Bulkrecipients

I can see that in the REST API documentation on bulkrecipients, the object recipientSignatureProviderInfo can be added to the JSON payload :
{
"bulkRecipients": [
{
"rowNumber": "sample string 1",
"email": "sample string 2",
"name": "sample string 3",
"note": "sample string 4",
"accessCode": "sample string 5",
"identification": "sample string 6",
"phoneNumber": "sample string 7",
"tabLabels": [
{
"name": "sample string 1",
"value": "sample string 2"
}
],
"recipientSignatureProviderInfo": [
{
"name": "sample string 1",
"value": "sample string 2"
}
]
}
]
}
But what is the correct way to add this to a text/csv content-type ?
for example :
Name,Email,Note,AccessCode,Identification,PhoneNumber,address1
David Jones,david.jones#yahoo.com,Here is the document we discussed.,,ID Check,,123 Main St
Kevin Smith,kevinmith#yahoo.com,,2243,,,697 My Way
Elisabeth Bozick,elisabeth.bozick#yahoo.com,,,phone,usersupplied,827 1st Ave
Whta is the correct header name ? I tried "recipientSignatureProviderInfo" but that didn't seem to work.
Thanks in advance !
The recipientSignatureProviderInfo column is not supported in the Bulk recipient CSV file.
Here is the link to Documentation
Only the following columns are supported.
Name
Email
Note
AccessCode
Identification
PhoneNumber
DocuSign Field Name
Additional resources.
https://support.docusign.com/guides/ndse-user-guide-send-a-document-using-bulk-send

JSON display method

I have scenario where I could use your help for the best practice and efficient code.
I have a JSON Array like below
[
{
"Date": "2014-07-16",
"DiscPoint": "Description 1",
"DisBy": "Person 1"
},
{
"Date": "2014-07-16",
"DiscPoint": "Description 2",
"DisBy": "Person 2"
},
{
"Date": "2014-07-16",
"DiscPoint": "Description 3",
"DisBy": "Person 3"
}
]
How do I omit the first element while iterating the JSONArray, Please note I have to omit it only after the first iteration.
I need to display the date for every new date. Please help me out in this scenario.
In $.each() function just add if-statement which will be skipping first index, which is equal to 0.
$.each(yourArray, function(index, value) {
if (index != 0) {
// do something
}
});