+1
Besvaret

How can I query an item in the top level of a JSON datasource?

thillsman 11 år siden opdateret af doug 10 år siden 4
Ok, so I'm diving deep into Parse for a project, and I'm using the example that John provided here: https://getsatisfaction.com/rarewire/...

I'm sending data, creating users, and doing all kinds of cool things. However, I want to see the response from Parse. I can see the raw response with [datasource:parsePost.0.content], and that's cool, but I want to see each piece broken out. For example, a Parse error response looks like this:

{
"code": 125,
"error": "invalid email a"
}

In this case, I tried to create a user with an email address of "a". Ideally, I'd just grab the "code" element, and use an < if> statement to tell the user that they need to use a real email address (if the code is "125").

Unfortunately, the response doesn't have "code" and "error" wrapped in another element, so I'm unsure how to query it.

In John's example he used query="/result" and [datasource:parsePost.0.content], but neither [datasource:parsePost.0.code] and [datasource:parsePost.1.code] work. I've also tried query="", query="/", query="//", and query="//*[local-name()]"; all work for [datasource:parsePost.0.content], but none work for either of my guesses on how to grab only the "code" element.

Hopefully that wasn't too confusing. :)
It should be a combination of a "/" query and [datasource:parsePost.1.code] similar to this example. If this isn't the case looking at your code might be helpful:


<wire>
<datasources>
<!-- add http header values the service needs here -->
<httpheader name="jsonheader" Accept="text/json"/>
<datasource name="validation" httpheader="jsonheader" source="" query="/" providertype="json" querycomplete="validate" httpmethod="post"/>
</datasources>
<main>
<!-- this text template contains the content you want to send to the service add [var:xxx] for variables etc -->
<texttemplate name="expired-receipt"><![CDATA[ {"format_version":1,"type":"email","email":{"to_email":"TO_ADDRESS","to_name":"TO_NAME","from_email":"FROM_EMAIL","from_name":"FROM_NAME","subject":"EMAIL_SUBJECT","body_html":"BODY_HTML_VERSION","body_text":"BODY_TEXT_VERSION"}}]]></texttemplate>

<panel name="body" height="50%" width="50%" background="#ff0000" valign="center" align="center">
</panel>
</main>
<actions>
<action name="start" oninit="yes">
<sync>
<!-- assign the value of the text template to the postContent property of the datasource to be sent to the server -->
<assign property="datasource:validation.postContent" value="[template:expired-receipt.content]" />
<!-- update the source of the ds to initiate the request -->
<assign property="datasource:validation.source" value="http://studio.rarewire.com/get-apple-subscription-data/D3mwuC8bvBWUXSnq" />
</sync>
</action>
<action name="validate">
<alert message="[datasource:validation.1.status]"/>
<assign property="var:status" value="[datasource:validation.1.status]" />
</action>
</actions>
</wire>
I attempted to use this query that you have listed above with / as the query for the login process, and using [datasource:parsePost.1.code] to get the status code (or any item for that matter), but with no luck. This is what I have:

<datasource name="parseLogin" httpmethod="get" source="" query="/" providertype="json" httpheader="parseHeader" urlcacheduration="1"></datasource>
<assign property="datasource:parseLogin.source" value="https://api.parse.com/1/login?username="[var:username]&password=[var:password] />
<action name="sign-in" datasource="parseLogin" datasourceevent="querycomplete">
			<alert message="[datasource:parseLogin.1.code]" />
		</action>
With the code above, all I have been getting is blank alerts...
Doug,

Were you able to get what you needed here once you encoded the & in your source? Or are you still having issues?
I am all good now. I just was having issues trying to access the JSON response from the server when I was logging in to Parse.

Kundesupport af UserEcho