Jump to content

Recommended Posts

Good morning.

Given the attached sample JSON file, if

     JSONGetElement($JSON ; "main.temp")

returns what I want, how do I format the JSONGetElement command to extract the data for Weather; Icon please - there is an extra "level" I don't know to access.

I'm sure it's just a character or two.

Thoughts please.

Greg

 

 

Sample.XML

Share this post


Link to post
Share on other sites
Posted (edited)

First, what you posted is not valid JSON. There's a comma after:

"temp" : 24.47

that shouldn't be there. If we remove it, then we can see that weather is an array that contains one object. You access this object using its index number (0-based) in square brackets:

weather[0]

and from there you can get the value of icon using:

 JSONGetElement($JSON ; "weather[0].icon")

 

--
P.S. Why does a file containing JSON have an .XML extension?

 

Edited by comment

Share this post


Link to post
Share on other sites

Hi Comment,

Thank you once again - that worked perfectly (not that I expected otherwise).

My error with the extension of the supplied file - I've been working with both XML and JSON files, so I just used the wrong extension - long, long nights.
With the extra comma in the supplied file, that result was what I got back from the API call.  Verbatim. I might need to contact the service and check with them. Error (aka lack of knowledge) was still with me, but that would have confused me even more. lol.

Thank you. I know your contribution to these forums is immensely appreciated. 😄 

Cheers,
Greg

Share this post


Link to post
Share on other sites
6 hours ago, Greg Hains said:

that result was what I got back from the API call.

That's too bad and you should definitely get them to fix it. But I am curious how it's possible that:

11 hours ago, Greg Hains said:

     JSONGetElement($JSON ; "main.temp")

returns what I want

For me, it returns an error:

? * Line 11, Column 2
  Missing '}' or object member name

 

Share this post


Link to post
Share on other sites

Hi Comment,

Yes, there was an error in the text I posted here. Apologies. Your solution worked well all the same.

At the risk of wearing out the welcome mat...

Whilst:
JSONGetElement( $$_JSON ; "list" )
JSONGetElement( $$_JSON ; "list[0].main.temp_min" )

retrieves that (minimum temperature data) OK, how do I access the data of weather ID please? It appears to be one level "deeper" within curly braces?  I tried:
JSONGetElement( $$_JSON ; "list[0].weather.id")
and too many guessed variations of this but failed to nail it.

Thank you again. :)

Greg

Sample.JSON

 

Share this post


Link to post
Share on other sites
Posted (edited)
8 hours ago, Greg Hains said:

how do I access the data of weather ID please? It appears to be one level "deeper" within curly braces?

The problem here is not curly braces, but square brackets. Same as with your previous question, these signify an array whose objects need to be accessed by their index number. In this case there is only one object, so the expression:

JSONGetElement ( $$_JSON ; "list[0].weather[0].id" )

will return 801.

Do note that list is also an array and it contains no less than 40 objects. The above expression extracts the weather id value from the first object only. Not all objects have the same weather id value, for example:

JSONGetElement ( $$_JSON ; "list[2].weather[0].id" )

returns 800.

 

How to solve such problems in general:

It is very convenient to use the JSONListKeys() function to analyze the given JSON and help to select the next step in your keyOrIndexOrPath expression. Starting with an empty path, the expression:

JSONListKeys ( $$_JSON ; "" )

returns:

city
cnt
cod
list
message

If you pick list, then:

JSONListKeys ( $$_JSON ; "list" )

will tell you there are 40 objects to pick from. If you pick the first one, then: 

JSONListKeys ( $$_JSON ; "list[0]" )

will return:

clouds
dt
dt_txt
main
sys
weather
wind

Next:

JSONListKeys ( $$_JSON ; "list[0].weather" )

returns "0". This is how we know it's an array containing a single object. And finally:

JSONListKeys ( $$_JSON ; "list[0].weather[0]" )

gives us:

description
icon
id
main

allowing us to construct the final path used above.

--
Note that the dots after the index predicate are optional; the expression:

JSONGetElement ( $$_JSON ; "list[0]weather[0]id" )

will work just as well.

 

Edited by comment
  • Like 1

Share this post


Link to post
Share on other sites

Hi Comment,

I appreciate the time you have spent explaining this to me. I have always preferred being taught the method than just being given the answer ("Give a man a fish, and you'll feed him for a day. Teach a man to fish, and you've fed him for a lifetime.") and you have provided me with lots of information here. 
Working with JSON is starting to make sense now. lol

Also, thank you BCooney. I tried this Generator tool recently but found it rather confusing to use as it split all the data up into different sections and you have to run the tool over each section. If you don't know JSON like I (don't) then I stayed just as confused. If you know JSON then you wouldn't need this tool - IMHO. As it is created by a knowledgable and reputable company I will put the confusion down to "it's me, not you" rather than product itself. :)  It didn't seem to give me the extra array components that confused me before, so now I know more about how it all work I will revisit it.

As always, I thank you both for helping me through this.

Cheers,
Greg

Share this post


Link to post
Share on other sites
8 hours ago, Greg Hains said:

I tried this Generator tool recently but found it rather confusing to use

I too couldn't understand what this file was about. I made myself a very simple tool - basically just:

image.png.464c3e272dc3c26cc29c7ed57ae3c2b3.png

You paste your JSON into the JSON field and build your keyOrIndexOrPath expression step-by-step by observing the result of cListkeys as explained above until cGetElement returns the expected result.

 

 

  • Like 1

Share this post


Link to post
Share on other sites

Hi Comment,

Thanks!  I will try this shortly and see if it "kicks in" for me. :) 

Cheers,
Greg

Share this post


Link to post
Share on other sites

Did you watch the video?

Share this post


Link to post
Share on other sites

Hi BCooney,

Tutorial on using the package makes a huge difference - no, I didn't watch it before (hangs head in shame).

Thanks,
Greg 

  • Like 1

Share this post


Link to post
Share on other sites
On 3/30/2020 at 3:55 AM, comment said:

I too couldn't understand what this file was about. I made myself a very simple tool - basically just:

image.png.464c3e272dc3c26cc29c7ed57ae3c2b3.png

Perfect explanation method.

I did watch the tutorial, as I found Generator first. I still was left rather perplexed by it.

OTOH, the sample WX JSON data and this explanation helped out much more.

Now to see where my syntax is breaking down while trying to get these results into variables.

 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.