how can i get the properties of a object? - javascript

how can i get the properties of my object?
hello i'm trying to get the properties with the array of ids but my console say undefined
i try with this
console.log(data.entry.properties)
it show me the two properties "pm:title" and "pm:taggable", but i only want the "pm:taggable" with his array
i try something like
console.log(data.entry.properties["pm:taggable"])
but it show me: properties of undefined (reading 'cm:taggable')
i´d like to show in console only the the array of ids 'cm:taggable'
data: {
entry: {
isFile: true,
createdByUser: [Object],
content: [Object],
aspectNames: [Array],
isFolder: false,
modifiedByUser: [Object],
name: 'Cine',
properties: [Object]
}
}
the properties have a object with
"properties": {
"pm:title": "Scary movie",
"pm:taggable": [
"a112a22463563-9c56-428f-a4ac-a2341as2a",
"2545ds467-62e5-448d-a062-34sf466dfs",
"a3af33-2b8d-40c3-98c3-3awfsa356a"
],
"cm:description": "Description"
}

Strange, it works for me. Perhaps you are incorrectly referring to the parent element, skipping the variable name?
const resourceData = {
"data": {
"entry": {
"id": "string",
"name": "string",
"nodeType": "string",
"isFolder": true,
"isFile": true,
"isLocked": false,
"modifiedAt": "2022-09-23T18:25:21.236Z",
"modifiedByUser": {
"displayName": "string",
"id": "string"
},
"aspectNames": [
"string"
],
"properties": {},
"allowableOperations": [
"string"
],
"properties": {
"pm:title": "title",
"pm:taggable": [
"a112a22463563-9c56-428f-a4ac-a2341as2a",
"2545ds467-62e5-448d-a062-34sf466dfs",
"a3af33-2b8d-40c3-98c3-3awfsa356a"
],
"cm:description": "Description"
}
}
}
}
console.log(resourceData.data.entry.properties['pm:title'])
console.log(resourceData.data.entry.properties['pm:taggable'])
console.log(resourceData.data.entry.properties['cm:description'])

Related

How to declare schema for at least one property is not null using JSON Schema?

const personData= {
name: null,
email: 'test#gmail.com'
}
const schema = {
instance: personData,
schema: {
type: "object",
anyOf: [
{ required: ["name", "email"] }
]
}
}
I want a schema which will validate the object and from object any of the key value (name or email) where one of them must be not null.
It looks like you're confused between "undefined" and "null", which are distinctly different. (I've now edited your question in light of your comment on my answer.)
The required keyword makes sure that a key is "defined" in the applicable object. The value is irrelevant, and may be null.
If you want to define the TYPE of a property, you have to use the type keyword.
anyOf must be an array of schemas where at least one of them must be true.
You've defined one subschema in the anyOf, and as a result, it must be true as a whole, making both items in the required array, required.
You want to define multiple schemas under your anyOf, where one each schema defines that a property must be of a specific type (null is a type).
{
"type": "object",
"required": ["name", "email"],
"anyOf": [
{
"properties": {
"name": {
"type": "string"
}
}
}, {
"properties": {
"email": {
"type": "string"
}
}
}
]
}
You can solve this by placing the 'required' property under the anyof list
{
"type": "object",
"anyOf": [
{
"required": ["name"],
"properties": {
"name": {
"type": "string"
}
}
}, {
"required": ["email"],
"properties": {
"email": {
"type": "string"
}
}
}
]
}
The required attribute means you must have a property in data. It may be nullable, though. To exclude null values make sure that anyOf properties you need are not null:
"anyOf": [
{
"properties": {
"name": {
"not": {
"type": "null"
}
}
}
},
{
"properties": {
"email": {
"not": {
"type": "null"
}
}
}
}
]
Full version goes below:
{
"type": "object",
"properties": {
"name": {
"type": [
"string",
"null"
]
},
"email": {
"type": [
"string",
"null"
]
}
},
"required": [
"name",
"email"
],
"anyOf": [
{
"properties": {
"name": {
"not": {
"type": "null"
}
}
}
},
{
"properties": {
"email": {
"not": {
"type": "null"
}
}
}
}
]
}

Need some help translating a get value in a JSON file

Our school receives data from a source during a sync.
I'm familiar with JavaScript but would like to ask for a litle help before I make a change.
Here is the scenario: The source sending the information to us has the default value as "tobedeleted". We need this to be translated to "inactivate". and then put into our DB.
What's being sent I think is simply ignored because it doesn't match any of our enum values.
My idea is to get help writing: if the get value = "tobedeleted" then translate it to "inactivate" and then update our database.
{
"path": "/v1/courses/{course_id}/enrollments/{id}",
"description": "Conclude, deactivate, or delete an enrollment. If the +task+ argument isn't given, the enrollment\nwill be concluded.",
"operations": [
{
"method": "DELETE",
"summary": "Conclude, deactivate, or delete an enrollment",
"notes": "Conclude, deactivate, or delete an enrollment. If the +task+ argument isn't given, the enrollment\nwill be concluded.",
"nickname": "conclude_deactivate_or_delete_enrollment",
"parameters": [
{
"paramType": "path",
"name": "course_id",
"description": "ID",
"type": "string",
"format": null,
"required": true,
"deprecated": false
},
{
"paramType": "path",
"name": "id",
"description": "ID",
"type": "string",
"format": null,
"required": true,
"deprecated": false
},
{
"paramType": "query",
"name": "task",
"description": "The action to take on the enrollment.\nWhen inactive, a user will still appear in the course roster to admins, but be unable to participate.\n(\"inactivate\" and \"deactivate\" are equivalent tasks)",
"type": "string",
"format": null,
"required": false,
"deprecated": false,
"enum": [
"conclude",
"delete",
"inactivate",
"deactivate"
]
}
],
"response_fields": [
],
"deprecated": false,
"deprecation_description": "",
"type": "Enrollment"
}
]
},
Thank you in advance!
Lets assign the JSON to a variable named data. Then you can do
data.operations.map((operation) => {
if (operation.method === 'DELETE') {
operation.parameters.map((param, queryIndex) => {
if (param.paramType === 'query') {
param.enum.map((item, enumIndex) => {
if (item === 'tobedeleted') {
operation.parameters[queryIndex].enum[enumIndex] = 'inactivate';
//item = 'inactivate';
}
});
}
});
}
return operation;
});
Note: This may not be an optimized code but it does the work.

How to consume the complex json nested properties

here i have a dynamic json
data = {
"name": "deltha",
"type": "object",
"important": [
"name",
"id",
"number"
],
"information": {
"place": {
"editable": false,
"visible": true
},
"info": {
"type": "object",
"properties": {
"type": {
"visible": true
}
}
},
"Image": {
"required": [
"name"
],
"type": "object",
"properties": {
"deltha": {
"search": "yes"
}
}
}
}
}
here i am trying to check whether each and every nested property has "required" attribute or not
for ex
data['information']["Image"]
here from the above object i have a attribute i have "required" and under that "name" is there
suppose like image how can i check each and every property to check there is 'required' if required there then how can i read that value dynamically
I'll suggest to use a recursive function, here is a working example : stackblitz.com/edit/angular-snucnm
use the hasOwnProperty check the property exist or not
let obj = data['information']["Image"];
if(obj.hasOwnProperty('required')){
console.log(obj.required)
}
you can check the availability of the property as follows,
if (data.information.Image.required !== undefined) {
console.log('prop is defined')
}

Validating JS Tests against a JSON Schema

I have an API which returns response in the format
[
{"id": 12345,
"value": "some_string",
"practice_id": "12344"},
{"id": 12346,
"value": "some_other_string",
"practice_id": "12345"},
]
I am testing that the response validates a specific JSON-Schema, and my schema test is
response.body.should.have.schema({
type: 'array',
required: ['id', 'value', 'practice_id'],
properties: {
id: {
type: 'number',
},
value: {
type: 'string',
},
practice_id: {
type: 'string',
minLength: 5,
}
}
});
The issue is that the test passes even if I change the type of id to string or change the value of practice_id to number, which is not correct.
What am I doing wrong here? I am using Postman-BDD to validate the responses.
I guess your schema should be more like this:
{
"type": "array",
"items":
{
"required":
[
"id",
"value",
"practice_id"
],
"properties":
{
"id":
{
"type": "number"
},
"value":
{
"type": "string"
},
"practice_id":
{
"type": "string",
"minLength": 5
}
}
}
}
You are missing the "items" keywords to actually define the content of the array. And this schema also gives an error in JSONBuddy on validating some sample data:

TypeError: Object [object Array] has no method 'getProperty'

I'm kind of new to this whole posting a question thing, so please be gentle!
I am using Breeze as part of the "Hot Towel" SPA stack and retrieving data from a custom WebApi endpoint written in PHP. It's not a full implementation, I've just written enough to give me what I need.
The metadata my endpoint is generating is as follows:
{
"shortName": "Project",
"namespace": "WebApi.ORM.Cartesius",
"autoGeneratedKeyType": "Identity",
"defaultResourceName": "Project",
"dataProperties": [
{
"name": "id",
"isPartOfKey": true,
"isNullable": false,
"dataType": "Int32"
},
{
"name": "title",
"isNullable": false,
"maxLength": 256,
"dataType": "String"
},
{
"name": "date",
"isNullable": false,
"dataType": "DateTime"
},
{
"name": "review_date",
"isNullable": true,
"dataType": "DateTime"
},
{
"name": "summary",
"isNullable": true,
"dataType": "String"
}
],
"navigationProperties": [
{
"name": "Team",
"entityTypeName": "Team:#WebApi.ORM.Cartesius",
"isScalar": true,
"associationName": "team_project_id_fkey",
"invForeignKeyNames": [
"project_id"
]
},
{
"name": "ProjectAuthor",
"entityTypeName": "ProjectAuthor:#WebApi.ORM.Cartesius",
"isScalar": true,
"associationName": "project_author_project_id_fkey",
"invForeignKeyNames": [
"project_id"
]
},
{
"name": "Itinerary",
"entityTypeName": "Itinerary:#WebApi.ORM.Cartesius",
"isScalar": true,
"associationName": "itinerary_project_id_fkey",
"invForeignKeyNames": [
"project_id"
]
},
Everything is working fine until I try and do an expand on my query:
var query = new breeze.EntityQuery()
.from("Project")
.where("id","eq",project.id)
.expand("ProjectAuthor");
This query returns the following from my endpoint:
[
{
"$id": 1,
"$type": "WebApi.ORM.Cartesius.Project",
"id": 2,
"title": "teat",
"date": "2013-11-04 14:00:00+07",
"review_date": null,
"summary": null,
"ProjectAuthor": [
{
"$id": 2,
"$type": "WebApi.ORM.Cartesius.ProjectAuthor",
"id": 1,
"account_id": 1,
"project_id": 2,
"Project": [
{
"$ref": 1
}
]
},
{
"$id": 3,
"$type": "WebApi.ORM.Cartesius.ProjectAuthor",
"id": 3,
"account_id": 2,
"project_id": 2,
"Project": [
{
"$ref": 1
}
]
}
]
}
]
Then Breeze chokes on it throwing:
TypeError: Object [object Array] has no method 'getProperty'
Debugging points to line 5059 in Breeze where it tries to getProperty on an array of entities rather than a single entity I assumed this had something to do with whether the navigation property was set to scalar or not but switching them around made no difference.
I'm sure I'm doing something massively wrong, but I can't figure out what it is and I've kind of hit a brick wall. I've read the docs from top to bottom and am implementing this as best as I can understand but it's possible I'm a little confused.
Thanks in advance for any help and apologies if I have not made myself clear or provided enough info
I think your problem is in the metadata
"name": "ProjectAuthor",
"entityTypeName": "ProjectAuthor:#WebApi.ORM.Cartesius",
"isScalar": true,
"associationName": "project_author_project_id_fkey",
"invForeignKeyNames": [
"project_id"
]
},
On that nav property Project->ProjectAuthor your telling it that its a scalar but its not in the data set..
"ProjectAuthor": [
{
..........
I've had this problem aswell .. 99% time its a metadata problem, if not there then start cutting metadata apart until you isolate the error .. though I'm sure thats the problem.. you need a hasMany there and an isScalar on the inverse navigation

Categories

Resources