playMaker

Author Topic: Data Maker JSON to XML Error[SOLVED]  (Read 660 times)

drown

  • Playmaker Newbie
  • *
  • Posts: 20
    • View Profile
Data Maker JSON to XML Error[SOLVED]
« on: January 11, 2017, 02:45:47 PM »
Hey guys!

I am currently learning how to fetch data from web APIs/Websites and turn them into Playmaker variables. I have an example where I get the current weather data from an API , convert it to variables and display it ingame. It works just fine.

However, for some JSON requests I get an error and some work just fine. If I try to turn this JSON request to a XML , it works fine :

Code: [Select]
{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"base":"stations","main":{"temp":278.24,"pressure":1010,"humidity":69,"temp_min":277.15,"temp_max":279.15},"visibility":10000,"wind":{"speed":6.7,"deg":260},"clouds":{"all":75},"dt":1484167800,"sys":{"type":1,"id":5187,"message":0.0169,"country":"GB","sunrise":1484121702,"sunset":1484151385},"id":2643743,"name":"London","cod":200}
I can acess this just fine. If, however, I get this response from the very same API for a differen location , I get an error while using the "Convert Json String To XML Node" - Action:

Code: [Select]
{"coord":{"lon":11.19,"lat":54.44},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"base":"stations","main":{"temp":278.132,"pressure":1003.42,"humidity":94,"temp_min":278.132,"temp_max":278.132,"sea_level":1003.75,"grnd_level":1003.42},"wind":{"speed":13.81,"deg":271.502},"rain":{"3h":1.0725},"clouds":{"all":76},"dt":1484169997,"sys":{"message":0.0028,"country":"DE","sunrise":1484119847,"sunset":1484147806},"id":8051091,"name":"Fehmarn","cod":200}
The error Unity displays is the following :

Code: [Select]
JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.
UnityEngine.Debug:LogError(Object)
HutongGames.PlayMaker.Actions.ConvertJsonStringToXmlNode:ConvertFromJsonString() (at Assets/PlayMaker DataMaker/Json/Actions/ConvertJsonStringToXmlNode.cs:102)
HutongGames.PlayMaker.Actions.ConvertJsonStringToXmlNode:OnEnter() (at Assets/PlayMaker DataMaker/Json/Actions/ConvertJsonStringToXmlNode.cs:62)
HutongGames.PlayMaker.FsmState:ActivateActions(Int32) (at c:/Users/Alex/Documents/Unity/Playmaker/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:201)
HutongGames.PlayMaker.FsmState:OnEnter() (at c:/Users/Alex/Documents/Unity/Playmaker/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:169)
HutongGames.PlayMaker.Fsm:EnterState(FsmState) (at c:/Users/Alex/Documents/Unity/Playmaker/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2603)
HutongGames.PlayMaker.Fsm:SwitchState(FsmState) (at c:/Users/Alex/Documents/Unity/Playmaker/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2561)
HutongGames.PlayMaker.Fsm:UpdateStateChanges() (at c:/Users/Alex/Documents/Unity/Playmaker/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2489)
HutongGames.PlayMaker.Fsm:UpdateState(FsmState) (at c:/Users/Alex/Documents/Unity/Playmaker/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2619)
HutongGames.PlayMaker.Fsm:Update() (at c:/Users/Alex/Documents/Unity/Playmaker/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:1865)
PlayMakerFSM:Update() (at c:/Users/Alex/Documents/Unity/Playmaker/Projects/Playmaker.source.unity/Assets/PlayMaker/PlayMakerFSM.cs:501)

The API offers direct XML , but I cant get it to work since it is formatted pretty weird and I cant get XPath to work there. I am gonna paste a quick example of the XML returns, if someone can tell me how to navigate to the values it would render my above question answered :

Code: [Select]
<current>
<city id="8051091" name="Fehmarn">
<coord lon="11.19" lat="54.44"/>
<country>DE</country>
<sun rise="2017-01-11T07:30:47" set="2017-01-11T15:16:47"/>
</city>
<temperature value="278.132" min="278.132" max="278.132" unit="kelvin"/>
<humidity value="94" unit="%"/>
<pressure value="1003.42" unit="hPa"/>
<wind>
<speed value="13.81" name=""/>
<gusts/>
<direction value="271.502" code="W" name="West"/>
</wind>
<clouds value="76" name="broken clouds"/>
<visibility/>
<precipitation value="1.0725" mode="rain" unit="3h"/>
<weather number="500" value="light rain" icon="10n"/>
<lastupdate value="2017-01-11T21:33:21"/>
</current>

Thanks in advance guys, I would really appreciate any kind of help!
 :)
« Last Edit: January 12, 2017, 11:46:50 PM by jeanfabre »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12351
  • Official Playmaker Support
    • View Profile
Re: Data Maker JSON to XML Error
« Reply #1 on: January 11, 2017, 11:03:24 PM »
Hi,

 First: I get this:

'3h' is not a valid XML Name
UnityEngine.Debug:LogError(Object)

so your json needs to be updated to get rid of any invalid Xml Node names. You simply do a string replace

once I did this, the json imported fine as an xml.

your error seems to be that the system returns a json that contains a list of objects, and xml can not do that, there must be a root node which then can contains a list.

I would recommend using their xml version, I don't see anything wrong with their formatting.

what info you want to get form that xml for example? I'll give you the xpath and you can then extrapolate on this.

Bye,

 Jean

drown

  • Playmaker Newbie
  • *
  • Posts: 20
    • View Profile
Re: Data Maker JSON to XML Error
« Reply #2 on: January 12, 2017, 01:52:25 AM »
Haven't thought about doing a String Replace, cant wait to try that!

I want the Max temperature and the wind direction code. I watched the Data Maker tutorial series on Youtube, but I cant match the xpath querys hes using to my usecase. Glad you are eager to help :)

Alex

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12351
  • Official Playmaker Support
    • View Profile
Re: Data Maker JSON to XML Error
« Reply #3 on: January 12, 2017, 11:46:10 PM »
Hi,

 ok, you need this:




-- access the current node first, with //current ( meaning get any "current" node, there's only one so we are fine)

and then you can get sub properties ( the context will be the "current" node selected above)

  -- /temperature/@max ( attributes starts with @)
  -- /wind/direction/@value

you can use  FsmFloat directly, not necessarly an FsmString, DataMaker will try to parse as a float and in this case it will be validate and work.


-- quick note: if you have more than one current node in the result, you'll need to get to it, by altering the xpath query

 - //current[2] will return the second "current" node found anywhere in your xml

 - using for example the city id, you would do this:



where you can insert in the xpath some fsm variable directly, using :_x_ to refer to the variables mentionned below the xpath query. Watch the "xpath query parsed" to make sure you got it right.


 Bye,

 Jean

drown

  • Playmaker Newbie
  • *
  • Posts: 20
    • View Profile
Re: Data Maker JSON to XML Error[SOLVED]
« Reply #4 on: January 13, 2017, 06:51:29 AM »
Thank you so much jean, that works perfectly fine! This is more of an answer than I have wished for since I can finally understand how exactly to use the xPath Query since I never heard of it before.

I am really happy right now!