How to read a JSON file?

Hello,

I would like to read a Json file in Daz script,but here's some problem!!

when I  tap the oData in the script, I got the right value -0.1102951 in the first print  and 0 in the second print.

var oData = {	"0Posx" : -0.1102951,	"0Posy" : 107.0425,	"0Posz" : -12.28755,	"0Rotx" : 0.02013095,	"0Roty" : -0.0003168428,	"0Rotz" : -0.007650683,	"0Sclx" : 0,	"0Scly" : 0,	"0Sclz" : 0}		var oSettings = new DzSettings();print( oSettings.getFloatValue( "0Posx" ))if( oSettings.fromString( JSON.stringify( oData ) ) ){	print( oSettings.getFloatValue( "0Posx" ) );	}}

but when I tried to read the oData from json file, use getFloatValue, I got 0 in the first print whatever the true value is  and nothing returned  in the second print.

var file = new DzFile("C:/Users/momo/Desktop/pose.json");var oData = file.read()var oSettings = new DzSettings();print( oSettings.getFloatValue( "0Posx" ))if( oSettings.fromString( JSON.stringify( oData ) ) ){	print( oSettings.getFloatValue( "0Posx" ) );	}}

I just want to read the value from a json file, please help!!

THANK YOU for replies!!

 

json
json
pose.json
33K

Comments

  • cridgitcridgit Posts: 1,757
    edited May 2022

    Redacted

    Post edited by cridgit on
  • cridgit said:

    You're mixing up DZSettings and JSON: you're reading data from a file which you're then stringifying with JSON which you're then trying to parse with DzSettings.

    Firstly, drop the DzSettings if you simply want to read/write JSON.

    Then, remember you want to stringify when writing JSON and parse when reading JSON. In this case you want to parse the text string you read from the file.

    See here for more info: http://docs.daz3d.com/doku.php/public/software/dazstudio/4/referenceguide/scripting/api_reference/object_index/json

     

    so, Is that right if I simply want to read a file? I got nothing in print1 and error carsed in print2 " Unable to parse JSON string"...

    var file = new DzFile("C:/Users/momo/Desktop/pose.json");var oData = file.read()print("print1:" ,oData)print("print2:" ,JSON.parse(oData))

     

  • so, Is is right if I simply want to read a file? I got nothing in print1 and error carse in print2 " Unable to parse JSON string"...

    var file = new DzFile("C:/Users/momo/Desktop/pose.json");var oData = file.read()print("print1:" ,oData)print("print2:" ,JSON.parse(oData))

     

  • You haven't opened the file for reading, which may not be helping

  • OmnifluxOmniflux Posts: 377
    edited February 2022

    The forum is having issues displaying code blocks right now, but if you click on "Quote", you will be able to see this properly formatted.

    var sFilename = "C:/Users/momo/Desktop/pose.json"; var oFile = new DzFile (sFilename); if (oFile.open (DzFile.ReadOnly)) { var oData = oFile.read(); print ("print1:", oData); print ("print2:", JSON.parse (oData)); } else print ("Error opening '%1': %2".arg (sFilename).arg (oFile.errorString()));
    Post edited by Omniflux on
  • Thanks for your replies!

    I can read json now but still have another question...

    I want to use the script to get the value by the key, like the example here,DzSettings [Documentation Center] (daz3d.com)

    but just got "0" in print( oSettings.getFloatValue( "0Posx" ) );

    var oSettings = new DzSettings();if( oSettings.toJsonString( JSON.stringify( oData ) ) ){	print( "Read of JSON encoded data was successful." ); 	print( oSettings.getFloatValue( "0Posx" ) ); // -0.11... } else {	print( "Could not read JSON encoded data." );}

     

  • OmnifluxOmniflux Posts: 377

    Not sure why you're still trying to use DzSettings, it has a specific purpose unrelated to what you've stated you are trying to do.

    var sFilename = "C:/Users/momo/Desktop/pose.json";/* JSON File contents{  "0Posx": -0.1102951,  "0Posy": 107.0425,  "0Posz": -12.28755,  "0Rotx": 0.02013095,  "0Roty": -0.0003168428,  "0Rotz": -0.007650683,  "0Sclx": 0,  "0Scly": 0,  "0Sclz": 0}*/sFilename = "E:/Test.json";var oFile = new DzFile (sFilename);if (oFile.open (DzFile.ReadOnly)) {	var oData;	try {		oData = JSON.parse (oFile.read());		if ('0Posx' in oData)			print ("'0Posx' = (%1) %2".arg (typeof (oData['0Posx'])).arg (oData['0Posx']));		else			print ("'0Posx' not defined.");	} catch (e) {		if (e instanceof SyntaxError)			print ("Error reading JSON file '%1': %2".arg (sFilename).arg (e.message));		else			throw e;	}} else	print ("Error opening '%1': %2".arg (sFilename).arg (oFile.errorString()));

     

  • I'm new to Daz script and use DzSettings because can't find other samples which function same like getFloatValue("0Posx")crying
    I tried the script you post here but got the error message Error reading JSON file 'C:/Users/momo/Desktop/pose.json': Unable to parse JSON.

  • Try using a known good file, such as a .duf preset, for testing - once that is working and you know your code is formally correct you can go on to hand-rolled (or custom saved) JSON files.

Sign In or Register to comment.