Message Boards Message Boards

0
|
6476 Views
|
6 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Import scientific notation with 3 digits in JSON?

Posted 8 years ago

Hello there, I am dealing with a problem because I have to import a json file with numbers in scientific notation and some of them have a value like: "position" : 5.2635442471208903129e-315 (in test2)

This value is reported after calculation of almost 0 value from a software in C++ but apparently Mathematica cannot use import such file ;(

Anyone have a clue how to solve this problem

 In[161]:= jsonImported = 
 SystemDialogInput["Directory", 
  WindowTitle -> "Select a json to import"]

 anglesData = Import[ jsonImported <> "test.json"]
 anglesData = Import[ jsonImported <> "test2.json"]

 Out[161]= "/Users/CNE/Documents/"

Out[162]= {"bananeScientific" -> 9.47983*10^-10, "pomme" -> 92.2902, "panier" -> {"oeuf" -> "4", "version" -> "4.2.900"},  "banane" -> 95.6774}

During evaluation of In[161]:= Import::fmterr: Cannot import data as JSON format. >>

 Out[163]= $Failed

Thank you

Chris

POSTED BY: Christophe Nell
6 Replies

oh, I didn't notice that 10^-300 does work but not 10^-320! But it doesn't solve my problem...

enter image description here

Here is an example of the type of file that I want to read with Mathematica. I am thinking about reading it as a string and when I have e-???, I change it by e-99. So somebody have a better idea or know an easy script to do that?

Attachments:
POSTED BY: Christophe Nell

Version 11.0.1 imports this perfectly fine:

{"pomme" -> 92.2902, "banane" -> -1.73601*10^-307}
POSTED BY: Sander Huisman

It's hard to comment without a sample. Please try to create an example JSON file based on your file that doesn't work.

JSON is kinda loose with how it specifies numbers, but it's supposed to be a subset of javascript. I don't think "5.2635442471208903129e-315" is valid syntax in javascript. The exponent is too big. So, as a random guess, maybe that has something to do with it. But I can't tell anything without an example of what of your problem.

POSTED BY: Sean Clarke

Here is a minimal example:

str=ExportString[{"AtomicWeight"->1.00793 10^-300},"JSON"]
ImportString[str,"JSON"]

str=ExportString[{"AtomicWeight"->1.00793 10^-320},"JSON"]
ImportString[str,"JSON"]
POSTED BY: Sander Huisman

@Sander Huisman, 5.2635442471208903129e-315 has the look and feel of a denormalized machine double. I don't know if that helps to explain anything here, just wanted to give an idea of what it is. Among other things this means how it gets handled can be platform dependent (not all support denormalized floats).

POSTED BY: Daniel Lichtblau

I'm not sure if the OP is still struggling with this or not. I just found out in the above reply that -300 is fine to import, but -320 is not fine. I'm not familiar with the concept op denormalized numbers; but a quick look at Wiki gave me some ideas what it is. There seems to be some 'range' of small numbers that import can't handle:

Table[
  {i, Check[ImportString[ExportString[{"Test" -> 1.0 10^-i}, "JSON"], "JSON"], "Fail"]}
,
 {i, 280, 330}
] // Grid

Any smaller exponent is just imported as '0.' It might, indeed, be related to these denormalized numbers, as it is at the 'end' of the small numbers. A Trace of such an import is very hard to read ;-)

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract