Jump to content

JSON question


This topic is 1401 days old. Please don't post here. Open a new topic instead.

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

Link to comment
Share on other sites

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
Link to comment
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

Link to comment
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

 

Link to comment
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

 

Link to comment
Share on other sites

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
Link to comment
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

Link to comment
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
Link to comment
Share on other sites

  • 1 month later...
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.

 

 

Link to comment
Share on other sites

This topic is 1401 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

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