Define JSON for requirement - javascript

I need to define a JSON for the following requirement;
Company company : Object (Name, Id)
User user – Object (Name, Age)
List<CustomObj> serviceList
o Service service
o String mode
o Boolean flag1
o Boolean flag2
I have tried something like;
{
Company : { "name" : "Company1", "id" : 123 },
User : {"name" : ["PV","PR", "DM", "TN"], "age" : null},
serviceList :
{
Service: {},
"mode" : null,
"flag1" : null;
"flag2" : null
}
}
The reason I have null values in a few places is because there are 2 modes; Create and Edit..
So in Create mode, some of the fields would have null values...
But you can let me know generically if it is correct?

Here's the answer before your edit, see below for a follow-up:
company looks like a simple object:
{"Name": "the name", "Id": "the ID"}
As does user:
{"Name": "the name", "Age": "the age"}
serviceList looks like an array of objects:
[
{
"service": {},
"mode": "the mode",
"flag1": true,
"flag2": false
},
...
]
You haven't shown what Service should look like, so I've just used {} above; it looks like it would be an object. The ... in the serviceList isn't literal, it's meant to indicate you'll probably repeat the thing above it.
So pulling those all together into a single JSON document:
{
"company": {"Name": "the name", "Id": "the ID"},
"user": {"Name": "the name", "Age": "the age"},
"serviceList": [
{
"service": {},
"mode": "the mode",
"flag1": true,
"flag2": false
},
...
]
}
Again, the .. in the serviceList is not literal, it indicates that you'll probably repeat the object above it.
You edited your question to add the
I have tried something like;
and the following. As you can tell from my original answer, I'd say the serviceList would be an array, not a single object, because List generally means exactly that: A list of something, which in JSON would be an array. You've also used lower-case identifiers where whatever it is you're quoting that you have to replicate is using initially-capped identifiers. JSON is case-sensitive, so that could be a problem.

Related

How can we access the Child Key from Object

I'm trying to loop my child array but it's not working. It's working fine which it's first items but not working on the child elements.
"candidateApplication": [
{
"label": "Are you currently attending school?",
"id": "ap_currentschool",
"value": "",
"required": "N",
"type": "radio",
"display": null,
"list": [
{
"searchCode": "yes",
"searchValue": "yes"
},
{
"searchCode": "no",
"searchValue": "no"
}
],
"onSelect": [
{
"yes": [
{
"label": "Estimated Completion Date",
"id": "ap_compdate",
"value": "",
"required": "N",
"type": "date",
"display": null,
"list": null,
"onSelect": null
}
],
"no": null
}
]
},
]
I am easily access my data to looping lists like obj.candidateApplication.list But I am not able to loop through obj.candidateApplication.onSelect.yes.
Can anybody please guide me how can I fix that issue.
enter image description here
You are accessing obj.candidateApplication.onSelect.yes which is undefined because obj.candidateApplication.onSelect is an array of objects.
You probably want to loop over obj.candidateApplication.onSelect and then for each object you can access the yes property as you wish:
$.each(onSelect, function (index, element) {
$.each(element.yes, function (x, y) {
// your code here
})
});
Edit: As pointed by Heritic Monkey in the comments, obj.candidateApplication is an array as well, but in the picture attached to the question, there's already a local variable called onSelect, loop through that instead of obj.candidateApplication.onSelect because it's undefined. (at least it should be, we don't know what your code is doing)

How to sent windows key for google cloud platform as an JSON?

I need to attach the encoded key to the modulus in for windows set metadata rest call to get a user validated for login.
So the format which google expects is:
{
"fingerprint": string,
"items": [
{
"key": string,
"value": string
}
],
"kind": string
}
So when you are sending data to google they want in this format.
{
"fingerprint": "sfasdfasdfFSN7AuU=",
"items": [
{
"key": "windows-keys",
"value": "{\"userName\": \"user\",
\"modulus\": \"somekey\",
\"exponent\": \"AQAB\",
\"email\": \"samleemail\",
\"expireOn\": \"2019-04-14T01:37:19Z\"
}"
}
]
}
It will also accept if I send the same JSON in this below format but will not perform any action in there:
{
"fingerprint": "asfasd",
"items": [
{
"key": "windows-keys",
"value":
{
"userName": "user",
"modulus": "IEFBQUFCM056YUMxeWM",
"exponent": "AQAB",
"email": "somemailt",
"expiresOn": "2019\"04-14T01:37:19Z"
}
}
]
}
Does any one knows a solution for this or faced this issue?
Google Doc link
In the format definition you're providing, it mentions that the value property of every item in the items array should be a string, and if you look closely, the "correct" example you provide sets value as a string`
{
"key": "windows-keys",
"value": "{\"userName\": \"user\",
\"modulus\": \"somekey\",
\"exponent\": \"AQAB\",
\"email\": \"samleemail\",
\"expireOn\": \"2019-04-14T01:37:19Z\"
}"
}
However, in your second example, you're setting value as an object, so it's not in the expected format, the correct version should look something like this:
{
"fingerprint": "asfasd",
"items": [
{
"key": "windows-keys",
"value": "{\"userName\": \"user\",
\"modulus\": \"IEFBQUFCM056YUMxeWM\",
\"exponent\": \"AQAB\",
\"email\": \"somemailt\",
\"expiresOn\": \"2019-04-14T01:37:19Z\"
}"
}
]
}
That should make it at least accept your request, you might need to fill the email with a correct one and check out whether if it's "expireOn" or "expiresOn" as your examples use both

Javascript gets undefined on array

I have the following data structure for javascript:
var data = {
"announcements": {
"IFT4S": [{
"id": "D7214",
"read_state": "unread",
"posted_at": "2018-10-25T14:35:54Z",
"title": "Reminder! Systems disruption: 27-28 Oct",
"message": "All University online systems will be unavailable."
}, {
"id": "B399C",
"read_state": "read",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Stem Fair",
"message": "The STEM Careers Fair is taking place on 31 October 2018"
}, {
"id": "6F5EE",
"read_state": "unread",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Smile more, worry less with our FREE course",
"message": "Take part in our Online Mindfulness Programme."
}]
}
}
I want to access the values of the keys "read_state", "posted_at", "title" and "message".
However, when I try data.announcements.IFT4S["title"] or any other key instead of "title" I get the undefined in the console.
What am I doing wrong?
when I try data.announcements.IFT4S["title"] or any other key instead
of "title" I get the undefined in the console. What am I doing wrong?
What you are doing here is trying to access the title key of the IFT4S array.
The issue is that IFT4S doesn't have a title key. Instead, like an array object, it has indexes as keys.
IFT4S = [ {...}, {...}, {...} ]
To access the first element of the IFT4S array you would do it like this
IFT4S[0]
In your case that would return the object at the first position of IFT4S array (index 0)
{
id: "D7214",
read_state: "unread",
posted_at: "2018-10-25T14:35:54Z",
title: "Reminder! Systems disruption: 27-28 Oct",
message: "All University online systems will be unavailable."
}
If you want to get all the titles from all the elements inside IFT4S array you could do this
IFT4S.map(element => element.title)
Array.prototype.map returns a new array where each element is the result of applying the function specified inside map to each element of the original array.
In this case, it would return
[
"Reminder! Systems disruption: 27-28 Oct",
"Stem Fair",
"Smile more, worry less with our FREE course"
]
You have to itrate over the array to get value from an array of object
var data = {
"announcements": {
"IFT4S": [
{
"id": "D7214",
"read_state": "unread",
"posted_at": "2018-10-25T14:35:54Z",
"title": "Reminder! Systems disruption: 27-28 Oct",
"message": "All University online systems will be unavailable."
},
{
"id": "B399C",
"read_state": "read",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Stem Fair",
"message": "The STEM Careers Fair is taking place on 31 October 2018"
},
{
"id": "6F5EE",
"read_state": "unread",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Smile more, worry less with our FREE course",
"message": "Take part in our Online Mindfulness Programme."
},
]
}
}
data.announcements.IFT4S.forEach(item => {
console.log(item.title)
})
or you can do like this,
0 is the index
console.log(data.announcements.IFT4S[0].read_state)
console.log(data.announcements.IFT4S[0].title)
IFT4S is an array, you can access its objects and their values values by calling:
data.announcements.IFT4S[index].title
with index beeing one of 0-2 here since the array contains 3 objects.
For example:
data.announcements.IFT4S[0].title
This is a very basic concept, check out any javascript guide to learn about arrays.

Using ng-repeat with keys in an array

I have an object, simulating a Cassandra database, where I retrieve its data to display it on a AngularJs app. Unfortunately, I can't make a premade header for my array because the keys are subject to change, depending on the data I'll retrieve (Here's an example of what I'm talking about):
var columnFamilyData = {
"DocInfo": {
"4c58abf5": {
"name": "coucou",
"extension": "pdf",
"size": 8751,
"type": "facture",
"repository": "archive"
},
"8cd524d7a45de": {
"name": "gerard",
"extension": "xml",
"size": 48734,
"type": "compta",
},
"5486d684fe54a": {
"name": "splendide",
"extension": "tiff",
"type": "photos",
"METADATA_A": "jambon"
}
},
"Base": {
"BASE_A": {
"name": "BASE_A",
"description": "That base is truly outrageous, they are truly, truly outrageous",
"baseMetadata_1": "METADATA_A",
"baseMetadata_2": "METADATA_B",
"baseMetadata_3": "METADATA_C"
},
},
}
As you can see, the arrays in DocInfo and Base are different, data and keys.
What I want to do is being able to use my ng-repeat to create a <th> line with the key name (for instance, Extension, of METADATA_A), but only once, since ng-repeat may duplicate this information.
I'm also providing a JSFiddle, if it can help any of you to understand my goal.
Thanks for reading and/or answering, have a great day.
Question is not totally clear, but it seems like you want "(key, val) in object" syntax.
ng-repeat="(name, age) in {'adam':10, 'amalie':12}"

Backbone.js design pattern for JSON data

I've got a question about best practice when designing JSON file which will be displayed by Backbone.js. I know that Backbone is completly agnostic in this topic, but maybe someone will give me good advice in this certain situation.
In the end, I need to have some views which will look like this
On 4th of July, in _____ we calebrate ____ day.
___ means a gap in text, where I'll have an text input or select (depends on type) which correctness will be verified.
So, I need to have a JSON file that describes that piece of text.
I thought about something like this
"body": [
{
"preInputText": "On 4th of July, in ",
"postInputText": "",
"answersID": ["1", "2"]
},
{
"preInputText": "we calebrate ",
"postInputText": " day",
"answersID": ["3"]
}
]
"answers": [
{
"ID": "1",
"content": "USA",
"correct": true
},
{
"ID": "2",
"content": "Canada",
"correct": false
},
{
"ID": "3",
"content": "Independent",
"correct": true
}
]
or, maybe simpleier, but not-so-flat
"body": [
{
"preInputText": "On 4th of July, in ",
"postInputText": "",
"answers": [
{
"ID": "1",
"content": "USA",
"correct": true
},
{
"ID": "2",
"content": "Canada",
"correct": false
},
]
}
]
etc…
So, first approach enforce creating two collections, passing them into one view, and checking values beetween them.
The second, just one collection of models that contains both body and answers, but parsing them at initialization and using nested construction.
I don't know is it a bad pratice (to use nested models), but as i read backbone was designed to think in the more flat way.
Maybe there is some kind of another logic? What do you think?
Thanks!
I'm more with the first approach (the flat one) and I don't agree with you that it enforce creating two collections.
You can always create a single collection and override it's parse function, something like this :
var MyCollection = Backbone.Collection.extend({
...
parse: function(resp) {
this.answers = new Backbone.Collection(resp.answers);
return resp.body;
}
});
...
// myCollection is an instance of MyCollection
myCollection.models // refer to questions
myCollection.answers // refer to answers
"body": [
{
"preInputText": "On 4th of July, in ",
"postInputText": "",
"answers" [ { "ID": "1", "content": "USA", "correct": "true"},
{ "ID": "1", "content": "canada", "correct": "false"}
]
},
{
"preInputText": "we calebrate ",
"postInputText": " day",
"answersID": [{ "ID": "3", "content": "Independent", "correct": "true"},
]
}
]
Using this structure, you need to use one collection. Then you can treat each object in this as a model and you can render these using their separate views in a collection view. So need to use nested models here

Categories