Implementing a JSON parser [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
This question is independent of implementation language.
In a recent interview I was asked to write a JSON parser :
The input I was given was something like below:
{
'key1': 'value1',
'key2': [key21, key22]
}
As simple as it may sound, I was stumped as I did not know how to write the parser (btw I am aware of JSON.parse() methods).
The question was to write your own parser.
It matters little that the above JSON is not in the right format. The parse should throw an error if it is not.
Can someone point me towards some technique I could have used to solve this problem.

Basically, you'll first have to scan through your string character by character in order to separate it into tokens.
{ and } are tokens. Let's call them 'START_OBJECT' and 'END_OBJECT';
[ and ] are also tokens. Let's call them 'START_ARRAY' and 'END_ARRAY';
: and , are tokens, too. Let's call them 'COLON' and 'COMMA';
'key1', 'key2' and 'value1' are 'STRING_CONSTANT' tokens, whose values are the strings themselves;
key21 and key22 are IDENTIFIER tokens, whose values are 'key21' and 'key22'
That initial part of the parsing process, where you break your source into tokens is called 'lexical analisis', and is the first step in the parsing process.
The next step would be the 'syntactical analysis', where you would figure out the actual structure of the source from the sequence of tokens.

Related

Need some help in understanding JavaScript datatypes [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Why JavaScript need datatypes?
I am trying to understand that what JavaScript will exactly do by knowing the type of data?
What is the benefit of having datatypes in JavaScript?
Based on the type of data, will there be any dynamic memory allocation happens OR there is any size limit like how some languages like C have (example: int : 2 bytes in C)?
All languages must implement some typing system. Javascript is dynamically typed meaning it assumes whatever type you assign to it (ex. var num = 5. Num assumes the type of "number").
By knowing the type of data, javascript will know which operations to perform on that data. For example: 2+2=4, but "2"+"2"=22.
You most likely don't have to worry about having a memory size limit for the number datatype. I don't know about strings, but numbers are infinite in javascript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management for info on memory management.

Generic vs Specific API Responses [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Any thoughts on how specific API response messages should be?
I'm looking at this from a Security perspective related to validating on data types.
Say my API requires string for an id and that my server validates the type - if not a string, should I response with something like.."Field must be of type string?".
This can be convenient to users who brushed by the documentation as it'll be a simple fix on their client code, but what about hackers?
They can kinda fish for information through these responses to learn more about the API inputs. I.e. they can input any random data and then find out the API only takes string which can help them even further.
Any thoughts on this?
Hiding things is never a good way to provide security.
You should provide as many details as possible about errors so you can help people work with your API. Your implementation should do every needed checks to ensure input data are safe.
Only specific point : do not throw errors like "Email does not exist in db" as it leaks information about your data.

Is there a way to get an Object from an Array Javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Coding in javascript, and I am trying to put objects into arrays, and the Array looks like
arr = [
{"id":1,"location":"Place1"},
{"id":2,"location":"Place2"}
]
I am trying to do arr[1].location, but arr[1] is undefined and console.log(arr[0]) and arr[1] in inspect element just returns as a "{". Im not sure if this is a chrome thing, but node does seem to accept the format.
Edit: I am collecting from a api that I made and this is the format it returns the code, I am just trying to access variable location. I am asking how would I get said variable
Problem answered in comments, didn't parse data.
Did you try parsing your data when received :
JSON.parse(apiResponse)
The problem was the way I handled the data. Instead of using a XMLHttpRequest for the array, I used jquery to request the data, and that seemed to work better for my API.

Is it possible to make a regex that accepts ANY substring [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm somewhat new to regex. I understand most of the basics but what I'm trying to do is beyond my knowledge, and may not even be possible.
I'm trying to make a regex in JavaScript that can match a series of function calls in the following pattern.
Name.Name(Params).Name(Params)
The names could be any standard java function name. I understand how do to this part. The params though can be different number of parameters (Currently only 0-2)
My biggest issue however is that params could potentially take ANY string with either a single or double quotation mark, or variable names. I have added some examples below as I need all of these to work with my regular expression (if Possible).
Examples:
Func.Foo().Bar()
Foo.Bar('foo', bar).Foobar()
Foo.Bar("foo", "bar").bar(')')
Foo.Bar('/"foo/"').bar("foo(bar/")")
My main concern here is I cant just look for a opening and parentheses or even 2 quotation marks.
Is it possible to use a regex so that I can parse the function call and parameters out?
The short answer to the Question in the title is yes, you can build a regex that matches any substring. But unfortunately that is not what you want. If you allow arbitrary substrings your regex will either match many cases you dont want to match or it will become extremely complex (see the email regex for an example).
What you want is a tokenizer!(https://medium.freecodecamp.org/how-to-build-a-math-expression-tokenizer-using-javascript-3638d4e5fbe9)
Edit: for the solutions in the comments: the ast parser is for java, the author wants to use javascript.

why we initialize array differently in php, javascript and etc [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
my question why we initialize array in different ways.
as i initialize array in php like array();
and in javascript in different way.
please anyone explain difference between array() and Array().
Thanks.
The shortest answer is that they are completely different languages and, just as different spoken languages have different grammar and vocabularies from each other, so do programming languages.
Beyond that, every programming language has to have some sort of runtime or compiler that understands the syntax, data structures, processing model, etc. And, each of those environments are free to implement those details as they see fit. This means that how an Array is internalized can be quite different between languages. But, to the programmer, we don't really need (or care) to know those implementation details.

Categories

Resources