Json Data Decoding without Library - json

I don't want to use a library. I want to figure out how to parse JSON data properly myself.
Example content :
If I was to parse this :
{"Name": [
{
"Type": "Type1",
"Content": "Content 1"
},
{
"Type": "Type1",
"Content": "Content 2"
},
{
"Type": "Type2",
"Content": "Content 3"
},
{
"Type": "Type2",
"Content": "Content 4"
}
]
}
Would I simply go about using indices and substrings and so on?
Or is there something about String manipulation that I am missing out on?

In javascript, eval() evaluates the expression. JSON is just a JS expression so it evaluates to an object. This is assuming the input is a valid JSON string. eval() runs all types of javascript code so beware of security.

Related

Validate JSON schema for expected values

I have a JSON which I would like to validate.
There are is an object inside an array, within each object there is a property called name.
I want 1st validate that there are 3 objects.
And I want to validate the value of each of the property.
{
"hello": [
{
"world": "value 1"
},
{
"world": "value 2"
},
{
"world": "value 3"
}
]
}
I want to validate that the JSON has value 1, value 2, value 3 using a JS0N schema
Using the language of JSON Extended Structural Schemas (JESS), the three requirements could be written in JSON as follows (assuming that you meant world rather than name):
["&",
{ "hello": [ {"world": "string"} ] },
{"forall": ".[hello]|length", "equal": 3 },
{"setof": ".[hello][]|.[world]", "supersetof": ["value 1", "value 2", "value 3" ]}
]
This may not be exactly what you want, e.g. perhaps you want the constraints to be written without reference to the name of the top-level key. This could be accomplished as follows:
["&",
{"forall": ".[]", "schema": [ {"world": "string"} ] },
{"forall": ".[]|length", "equal": 3 },
{"setof": ".[][]|.[world]", "supersetof": ["value 1", "value 2", "value 3" ]}
]
Also you could modify the above to express the requirements without preventing the objects from having additional keys. It all depends on what you really want.
Note that the JESS checker requires jq to run. There is a ruby gem for jq.

Why does a numeric key in the JSON Structure always get displayed first

(Cannot summarize the problem in a single statement, hence the ambiguous title)
I create a JSON structure via Angular Typescript, wherein when a user interacts with certains parts of the component the JSON Structure gets updated.
Steps
Initially, the JSON under consideration is by default set to the following:
{
"keyword": {
"value": "product",
"type": "main"
}
}
For example, a user chooses some parameter Name. Once the user complies to certain steps in the UI, the JSON structure gets updated to the following:
{
"keyword": {
"value": "product",
"type": "main"
},
"Name": {
"value": " <hasProperty> Name",
"type": "dataprop"
}
}
Once the user selects a numeric value for a parameter like dryTime, the JSON gets updated to the following:
{
"20": { // WHY WOULD 20 be here?
"value": "<hasValue> 20",
"type": "fValue"
},
"keyword": {
"value": "Varnish",
"type": "main"
},
"Name": {
"value": " <hasProperty> Name",
"type": "dataprop"
},
"dryingTime": {
"value": " <hasProperty> dryingTime",
"type": "dataprop"
}
}
I understand that a JSON is an unordered data structure. But a previous implementation of something similar actually worked well, i.e., the value 20 here was 20.0 before and it was displayed after dryingTime in my JSON.
The order is critical for me as I parse all the Keys in the above mentioned JSON using a for loop and store it in an array. This array needs to show all the keys in the order of the User Interaction.
Where am I going wrong here if I decide to stay with JSON and not with an array to store such interactions?
Yes, JSON fields are unordered. JSON array is ordered.
If you want to keep the order of elements insterted, you could build your JSON like so:
{
"keyword": {
"value": "Varnish",
"type": "main"
},
"props": [
{
"name": "dryingTime",
"value": 20
},
{
"name": "anotherOrderedField",
"value": "fieldValue"
}
]
}

Seggregation of Json Schema Validation and Json Validation

I have a use case where I will take a json-schema as input, validate it, then keep in my system. Later I will get json data which I need to validate with above mentioned json-schema. given the scenario, I need to do two level of validations:
1. provided json-schema is valid or not.
2. Json is valid or not.
I am using json-schema-validator jar and could find only second level of validation, couldn't find json-schema validation in documentation. for example: lets say we have below sample json-schema:
{
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
"required": ["firstName", "lastName"]
}
so how to validate this json-schema itself is valid or not?
There is a working example here with the everit-org/json-schema implementation (just in case you want to use a maintained library):
How to validate a json schema against the version spec it specifies in Java
You have to validate schema against meta-schema: http://json-schema.org/draft-04/schema

How do i document optional RESTful JSON API attributes?

I've been trying to figure out how to design the documentation for an API I'm building. I've been using Swagger (swagger.io) along with JSONSchema to help me structure the documentation, but I've run into a snag. Some of our object will have lots of what we call "metadata" attached to them, which is basically an arbitrary and variable dictionary of key/values. For example:
{
"id": "aabbbccdd",
"name": "Object 1",
"metadata": {
"attributeA": "This is a text attribute",
"attributeB": {
"key1": "complex attribute",
"key2": "complex attribute 2",
},
"attributeC": 1234
}
},
{
"id": "eeffffggghh",
"name": "Object 2",
"metadata": {
"attributeA": "This is a text attribute with a different value",
"attributeD": "Another text attribute",
"attributeE": True
}
}
So I could document the object representation by enumerating all the metadata attributes, but the list is long and will likely vary over time.
Is there another way to approach documenting this, or designing the API in a different way?

Accepting an image as an input by reading a JSON schema?

In a JSON Schema we can specify what type of entity we are expecting such as a string like
"Name": {
"type": [
"string",
"null"
]
}
Can we do something same for expecting images as input?
JSON Hyper-Schema defines a media keyword that can allow you to specify an image as an input. Most JSON Schema validators don't support Hyper-Schema, but if you have one that does, this could be useful.
{
"type": "string",
"media": {
"binaryEncoding": "base64",
"type": "image/png"
}
}
http://json-schema.org/latest/json-schema-hypermedia.html#anchor10