• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Why does parsing JSON in Java Script not require file such as POJO or POCO?

 
Ranch Foreman
Posts: 2015
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we parse JSON in Java we require a POJO file and in C# we require POCO file. In Java Script we can direct parse and it does not require such a file. Why?
Thanks
 
Saloon Keeper
Posts: 22779
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key is in the name: JavaScript Object Notation.

JSON data is in the exact same format that you'd define it in JavaScript code. So all that's necessary to use it is to evaluate it.

In fact, it's such a direct action that sometimes things have to be done to ensure that no one injects bad things in along with the data.
 
Monica Shiralkar
Ranch Foreman
Posts: 2015
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:The key is in the name: JavaScript Object Notation.

JSON data is in the exact same format that you'd define it in JavaScript code.



how is the format different in case of Java?
 
Saloon Keeper
Posts: 6703
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not different - JSON is the same for all languages. It's just baked into JavaScript, whereas Java needs to use a library to handle it.

While I'm not sure what you mean by "POJO file", note that no actual file is required for Java (or an language) to work with JSON. Most often, it is sent and consumed directly over the wire.
 
Monica Shiralkar
Ranch Foreman
Posts: 2015
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By POJO I meant Plain Old Java Object which has the getters and setters. For example if json has name and age of employee I can use Jackson library and store the result in the object of type employee. The class Employee will be having getter setter for name and age. Why is no such class with getters and setters is required for Java Script?
 
Saloon Keeper
Posts: 12484
269
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaScript is dynamically typed and based on prototypes. You can add a property to an object without having to declare the property:

That means that JavaScript can also easily parse and format JSON, because for each JSON property it discovers, it just has to assign the value to an object property, whether the object property already exists or not.

Secondly, as Tim already pointed out, JSON was specifically designed for JavaScript. It reflects object literals in the language itself, so JavaScript can use the built-in language parser to read JSON.

Finally, there is no real point in comparing the two, because Java and JavaScript are wildly different languages that have few things in common.
 
Tim Holloway
Saloon Keeper
Posts: 22779
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSON wasn't "designed" for JavaScript. JSON literally is JavaScript.

You can easily see this because you can copy and paste JSON into a JavaScript script without any function/method calls or syntax changes.

If I give you something like this:

There is absolutely no way you can tell whether that's supposed to be a JSON web service request or response, or just part of a JS script:
 
Stephan van Hulst
Saloon Keeper
Posts: 12484
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:JSON wasn't "designed" for JavaScript. JSON literally is JavaScript.


Until 2019, it literally wasn't.
 
Tim Holloway
Saloon Keeper
Posts: 22779
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Tim Holloway wrote:JSON wasn't "designed" for JavaScript. JSON literally is JavaScript.


Until 2019, it literally wasn't.



Would you care to elucidate?
 
Monica Shiralkar
Ranch Foreman
Posts: 2015
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:

There is absolutely no way you can tell whether that's supposed to be a JSON web service request or response, or just part of a JS script



By part of a Java Script does it mean Java Script Object?
 
Monica Shiralkar
Ranch Foreman
Posts: 2015
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:JavaScript is dynamically typed and based on prototypes. You can add a property to an object without having to declare the property:



This exactly answers the question. I was thinking that for us to be able to store the parsing response in car object we need to have a class for it the populated object of which we can use again and again in the application. In Java Script,  we do not need to define in advance what variables the class will have the type of which is car. So we can store it in a variable and use the variable whereever we want in the application instead of first defining the class for that type with setters for color etc.

Thanks
 
Stephan van Hulst
Saloon Keeper
Posts: 12484
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:Would you care to elucidate?


Until the ECMAScript standard became more permissive in 2019, you weren't allowed to use some Unicode whitespace characters in JavaScript object literals that you were allowed to use in JSON, at least not without escaping them. That means that with older JavaScript versions, it was possible to write JSON that would not be valid JavaScript.
 
Monica Shiralkar
Ranch Foreman
Posts: 2015
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You can add a property to an object without having to declare the property.



This saves lot of coding and makes it such easy. I wonder why Java doesn't come up with a feature like this some day.
 
Stephan van Hulst
Saloon Keeper
Posts: 12484
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it comes with its own share of problems. For instance, you either have to trust that a certain property always exists on a given object, or you have to check for it before you access it. In Java, if you know an object is of a certain type, you know for certain that you can access the property. I've had so much bugs with dynamic typing that I automatically think of any language that uses it as inferior.

I'm obviously not the only one, as there are many dialects of JavaScript that aim to bring static typing to the language, such as TypeScript.
 
Tim Holloway
Saloon Keeper
Posts: 22779
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:
This saves lot of coding and makes it such easy. I wonder why Java doesn't come up with a feature like this some day.



Because you can get yourself in a lot of trouble that way. Java is strongly-typed and thus the compiler can detect a lot of mistakes that in interpreted languages might sit unknown for literally years before triggering with distastrous effect.

I actually have encountered "time bomb" code, myself, At 3 am when the data center supervisor called me in because people had been using a data compression library with a hidden flaw in it that happened to be sensitive to an OS update/bug fix.

 
Tim Holloway
Saloon Keeper
Posts: 22779
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:
Until the ECMAScript standard became more permissive in 2019, you weren't allowed to use some Unicode whitespace characters in JavaScript object literals that you were allowed to use in JSON, at least not without escaping them. That means that with older JavaScript versions, it was possible to write JSON that would not be valid JavaScript.



I see. Possible, but highly unlikely. I don't think JavaScript - which isn't quite the same thing as ECMAScript - was originally designed for Unicode at all. Straight ASCII, at the time, I'm pretty sure. So that particular wart would have come in via the back door.

You're technically correct, but I doubt that many people noticed.
 
Stephan van Hulst
Saloon Keeper
Posts: 12484
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:You're technically correct


The best kind of correct.
 
Monica Shiralkar
Ranch Foreman
Posts: 2015
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Then YOU must do the pig's work! Read this tiny ad. READ IT!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic