# Json question on getting element

**URL:** <https://the.fmsoup.org/t/json-question-on-getting-element/923>\
**Category:** Questions\
**Created:** [May 13, 2020, 6:29pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923 "2020-05-13T18:29:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Susurrus](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/susurrus/32/472_2.png) [@Susurrus](https://the.fmsoup.org/u/Susurrus)\
**Post date:** [May 13, 2020, 6:29pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/1 "2020-05-13T18:29:44Z")

</div>

I am trying to figure out JSON and things just do not seem to be clicking.

I am able to make this JSON code form my data.

```
{
	" 2016 " : 
	{
		"Improvements" : 78793900,
		"Land" : 40853000
	}
}

```

I am trying to set a filed with the land number.

Set Field [cards\_REPORT::Jason Test; JSONGetElement ( cards\_REPORT::Jason Test 2 ; "2016[1].Land")]

this gives me a blank filed. I was hoping for 40853000 what am I doing wrong?

---

<div class="post-metadata">

**Author:** ![WimDecorte](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@WimDecorte](https://the.fmsoup.org/u/WimDecorte)\
**Post date:** [May 13, 2020, 6:56pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/2 "2020-05-13T18:56:31Z")

</div>

You're trying to get the 2nd element (index 1 - arrays are 0-based) of an array but there is no array.

Assuming that the spaces around 2016 are just bad formatting then this should work:

Set Field [cards\_REPORT::Jason Test; JSONGetElement ( cards\_REPORT::Jason Test 2 ; "2016.Land")]

---

<div class="post-metadata">

**Author:** ![Susurrus](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/susurrus/32/472_2.png) [@Susurrus](https://the.fmsoup.org/u/Susurrus)\
**Post date:** [May 13, 2020, 7:01pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/3 "2020-05-13T19:01:52Z")

</div>

Thanks WimDecorte

I have been working in FileMaker and C++ so long I am not at all accustomed to caring about spaces. I will look and see why the 2016 is getting formatted with spaces but it looks like that is why I was not able to get it to work.

I am not sure why but JSON is very confusing for me to rap my head around. But the potential is really exciting.

---

<div class="post-metadata">

**Author:** ![Bobino](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/bobino/32/194_2.png) [@Bobino](https://the.fmsoup.org/u/Bobino)\
**Post date:** [May 13, 2020, 7:03pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/4 "2020-05-13T19:03:52Z")

</div>

Do not hesitate to reach out if anything does not seem to be working the way you expect. We will be happy to help.

---

<div class="post-metadata">

**Author:** ![Susurrus](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/susurrus/32/472_2.png) [@Susurrus](https://the.fmsoup.org/u/Susurrus)\
**Post date:** [May 13, 2020, 7:54pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/5 "2020-05-13T19:54:11Z")

</div>

Is this the best way to get 2016 or 2017 element? Or is there a JSON function to get it?

GetValue ( JSONListKeys ( cards\_REPORT::Jason Test 2; "" ); 1)

```
{
	"2016" : 
	{
		"Improvements" : 78793900,
		"Land" : 40853000
	},
	"2017" : 
	{
		"Improvements" : 78793900,
		"Land" : 40853000
	}
}

```

_Edited out the [1] as it was not needed_

---

<div class="post-metadata">

**Author:** ![WimDecorte](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@WimDecorte](https://the.fmsoup.org/u/WimDecorte)\
**Post date:** [May 13, 2020, 8:03pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/6 "2020-05-13T20:03:42Z")

</div>

If that is how you get JSON then it sucks at little because you'd expect an array with elements for each year. But as it is, use the JSONListKeys () function to get a list of the years and then loop through that list and ask for the land element of each one of the key's value.

---

<div class="post-metadata">

**Author:** ![Susurrus](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/susurrus/32/472_2.png) [@Susurrus](https://the.fmsoup.org/u/Susurrus)\
**Post date:** [May 13, 2020, 8:10pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/7 "2020-05-13T20:10:39Z")

</div>

Well right now how I do things are in flux and I am hoping to get guidance on how best to do it.

What I am trying to do as my first attempt at JSON is to have a a graph populated from JSON with some basic information. a list of years and amounts for Land and Improvements for each year. I feel like I am about 70% there on it functioning but about 30% for understanding.

You lost me a little on your last response but it seems like I might not be formatting my original JSON correctly?

---

<div class="post-metadata">

**Author:** ![WimDecorte](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@WimDecorte](https://the.fmsoup.org/u/WimDecorte)\
**Post date:** [May 13, 2020, 8:14pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/8 "2020-05-13T20:14:34Z")

</div>

Are you creating the json? Or parsing it? I may have misunderstood.

If you are creating the JSON then you would create this as an array of JSON elements  
Each element would look like this:

```
{
    "year": 2006,
	"Improvements" : 78793900,
	"Land" : 40853000
}
```

---

<div class="post-metadata">

**Author:** ![WimDecorte](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@WimDecorte](https://the.fmsoup.org/u/WimDecorte)\
**Post date:** [May 13, 2020, 8:17pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/9 "2020-05-13T20:17:02Z")

</div>

and your whole construct would look like this:

```
[{element 1}, {element 2}, {element 3}]
```

---

<div class="post-metadata">

**Author:** ![Susurrus](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/susurrus/32/472_2.png) [@Susurrus](https://the.fmsoup.org/u/Susurrus)\
**Post date:** [May 13, 2020, 8:27pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/10 "2020-05-13T20:27:22Z")

</div>

I am creating and formatting my own JSON filling in the data from my database. And it sounds like I am doing it wrong.

**This is what I am currently using.**

```
JSONFormatElements ( 
                     "{
                       \""& $Year &"\" : 
                          { 
                            \"Land\" : "&$land&",
                            \"Improvements\" : "&$Improvements&"
                          } 
                       }"    
                    )

```

**Is this what I should be doing?**

```
JSONFormatElements ( 
                     "{
                            \"Year\" : "&$Year&",
                            \"Land\" : "&$land&",
                            \"Improvements\" : "&$Improvements&"
                       }"    
                    )
```

---

<div class="post-metadata">

**Author:** ![WimDecorte](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@WimDecorte](https://the.fmsoup.org/u/WimDecorte)\
**Post date:** [May 13, 2020, 8:42pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/11 "2020-05-13T20:42:52Z")

</div>

Ugh, no! Don't construct the JSON by concatenating text, do this:

JSONSetElement ( "{}" ;  
["year" ; $year ; jsonnumber] ;  
["land" ; $land ; jsonstring] ;  
["Improvements" ; $Improvements ; jsonstring]  
)

---

<div class="post-metadata">

**Author:** ![Susurrus](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/susurrus/32/472_2.png) [@Susurrus](https://the.fmsoup.org/u/Susurrus)\
**Post date:** [May 13, 2020, 9:00pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/12 "2020-05-13T21:00:24Z")

</div>

Couple questions.

- The $land and $Improvements are dollar amounts I am guessing I should have them also as jsonnumber?
- I am assuming I will need to assign a element in the "{}" ; I tried a couple things without any luck. How would I do that?

Or am I missing completely how one would reference each element?

---

<div class="post-metadata">

**Author:** ![WimDecorte](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@WimDecorte](https://the.fmsoup.org/u/WimDecorte)\
**Post date:** [May 13, 2020, 9:09pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/13 "2020-05-13T21:09:46Z")

</div>

Yes on the json number format.  
I don't understand your second question.  
If you mean "how do I then build the array?" then do this

I'm assuming you're looping through something to get the data

set variable[$x ; 0]  
set variable[$array ; "[]" ]  
Loop

set variable[$json ; that JsonSetElement that builds the json from the previous message]  
set variable[$array ; $x ; $json ; jsonobject]

Exit Loop If[whatever your condition is]  
Set Variable [$x ; $x + 1]

End Loop

---

<div class="post-metadata">

**Author:** ![Susurrus](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/susurrus/32/472_2.png) [@Susurrus](https://the.fmsoup.org/u/Susurrus)\
**Post date:** [May 14, 2020, 6:37pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/14 "2020-05-14T18:37:31Z")

</div>

With some more research and some trial and error I was able to get it to work I think. Below was the result. Be fore I proceed can you confirm this looks good? If this dose look good I will start working on putting it into a chart. Thank you for all your help you really pointed me in the right direction.

```
{
	"0" : 
	{
		"Improvements" : "74744600",
		"land" : "37661500",
		"year" : 2016
	},
	"1" : 
	{
		"Improvements" : "76235700",
		"land" : "38465000",
		"year" : 2017
	},
	"2" : 
	{
		"Improvements" : "80003600",
		"land" : "39558100",
		"year" : 2018
	},
	"3" : 
	{
		"Improvements" : "81044900",
		"land" : "39727900",
		"year" : 2019
	},
	"4" : 
	{
		"Improvements" : "78793900",
		"land" : "40853000",
		"year" : 2020
	}
}
```

---

<div class="post-metadata">

**Author:** ![WimDecorte](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@WimDecorte](https://the.fmsoup.org/u/WimDecorte)\
**Post date:** [May 14, 2020, 6:42pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/15 "2020-05-14T18:42:35Z")

</div>

That is an odd construct because it sorta mimics an array. Why not just use an array?

---

<div class="post-metadata">

**Author:** ![WimDecorte](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@WimDecorte](https://the.fmsoup.org/u/WimDecorte)\
**Post date:** [May 14, 2020, 6:44pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/16 "2020-05-14T18:44:36Z")

</div>

> [@WimDecorte](#):
>
> set variable[$array ; ""]

perhaps you missed that part in my pseudo script. The square brackets instead of the curly brackets is what you initiate the array with.  
Based on your output you may have used curly brackets; change that to square brackets and you will end up with a proper array.

---

<div class="post-metadata">

**Author:** ![Susurrus](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/susurrus/32/472_2.png) [@Susurrus](https://the.fmsoup.org/u/Susurrus)\
**Post date:** [May 14, 2020, 10:48pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/17 "2020-05-14T22:48:46Z")

</div>

Well I am glad I continued posting my results and it proves I still have a lot to learn.

If I change $array to "[]" instead of "{}" it looks like this. If this is correct I am not sure how one would reference the different elements? I think that is what they are called.

```
[
	{
		"Improvements" : "74744600",
		"land" : "37661500",
		"year" : 2016
	},
	{
		"Improvements" : "76235700",
		"land" : "38465000",
		"year" : 2017
	},
	{
		"Improvements" : "80003600",
		"land" : "39558100",
		"year" : 2018
	},
	{
		"Improvements" : "81044900",
		"land" : "39727900",
		"year" : 2019
	},
	{
		"Improvements" : "78793900",
		"land" : "40853000",
		"year" : 2020
	}
]

```

**This is my code that gets the above.**

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/fmsoup/original/1X/4c585c4aa1b8f7e20f2e740ba78ae9573ef9e843.png)  
**for setting $json**

```
JSONSetElement ( "{}" ;
                    ["year" ; $year ; JSONNumber] ;
                    ["land" ; $land ; JSONString] ;
                    ["Improvements" ; $Improvements ; JSONString]
               )

```

**for setting $array**

`JSONSetElement ( $array ; $X ; $json; JSONObject)`

---

<div class="post-metadata">

**Author:** ![Bobino](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/bobino/32/194_2.png) [@Bobino](https://the.fmsoup.org/u/Bobino)\
**Post date:** [May 14, 2020, 11:29pm UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/18 "2020-05-14T23:29:46Z")

</div>

This looks excellent.

Your $array is a structure you can read from in a loop by asking for a specific entry in the following fashion to retrieve your json object:

```
JSONGetElement ( $array ; "[0]" )

```

JSON arrays are 0 based, so asking for entry 0 gives you the first entry of the array.

You can retrieve individual values from your $array by giving the corresponding path:

```
JSONGetElement ( $array ; "[4].year" )

```

The above will retrieve the year for the 5th object in your array.

If you are dealing with a large array, parsing in a loop for different keys using the full path from the root can tax you performance wise. It can be preferable to retrieve the whole array entry in a variable and then parse out other elements from the nested keys referring to your new variable.

Always keep in mind that keys are case-sensitive.

You seem to be on the right path (pun intended).

If anyone else wants to add something I might have missed or correct me, please do so.

---

<div class="post-metadata">

**Author:** ![Susurrus](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/susurrus/32/472_2.png) [@Susurrus](https://the.fmsoup.org/u/Susurrus)\
**Post date:** [May 15, 2020, 12:12am UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/19 "2020-05-15T00:12:51Z")

</div>

I am now able to grab all the data. I mistakenly thought you would need them to be defined to grab 0-4 but it looks like it works without. Thank you @Bobino for that explanation. I will play around with this some more and probably come back with question on how best to put this data into a chart. But this gives me a lot to work with for now.

Thanks you everyone for your help

---

<div class="post-metadata">

**Author:** ![planteg](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/planteg/32/627_2.png) [@planteg](https://the.fmsoup.org/u/planteg)\
**Post date:** [May 15, 2020, 12:53am UTC](https://the.fmsoup.org/t/json-question-on-getting-element/923/20 "2020-05-15T00:53:05Z")

</div>

If you wish to check JSon data, use [http://jsonviewer.stack.hu/](http://jsonviewer.stack.hu/) which is a JSon Viewer: on the Text Tab type/paste the JSon Data and the switch to the Viewer Tab to see what you get. Errors are flagged !

A very useful tool.

[Next page](https://the.fmsoup.org/t/json-question-on-getting-element/923.md?page=2)
