Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays in arrays #202

Closed
jacobwilliams opened this issue Jun 10, 2016 · 4 comments
Closed

Arrays in arrays #202

jacobwilliams opened this issue Jun 10, 2016 · 4 comments

Comments

@jacobwilliams
Copy link
Owner

jacobwilliams commented Jun 10, 2016

A question from an email:

Hi Jacob,
First of all thanks for your json fortran api.
I am facing an issue when trying to create an array “Waypoints” within an array “ValidatorRequests” to get the following structure:

Objective:

{
  "ValidatorRequests": [
    {  
  "Waypoints": [
    {
      "Lat": 45.0,
      "Lon": -60.0,
      "DelayInHours": 0.0
    },

However when I create an array within an array, the array name , here “Waypoints” in yellow above is missing below.
Do you have any idea what’s going on here ? I am really new to json … so I guess it is trivial but I can’t see it.

Result of my code:

{
  "ValidatorRequests": [
[
    {
      "Lat": 0.39E+2,
      "Lon": -0.1E+2,
      "DelayInHours": 0.0E+0
    },

And here my code

type(json_value),pointer :: e,f,g,h,json_AnalysisIssues,root
type(json_core) :: jsonc       
…
call jsonc%add(root,f) !add f to the root structure
nullify(f) !no longer need this

! 3.3 WayPoints 
call jsonc%create_array(f,'ValidatorRequests')
call jsonc%create_array(e,'WayPoints')
do i=1,WayPointsNumber
  call jsonc%create_object(h,'')
  call jsonc%add(h,'Lat',latWayPoints(i))
  call jsonc%add(h,'Lon',lonWayPoints(i))
  call jsonc%add(h,'DelayInHours',DelayInHoursWayPoints(i))
  call jsonc%add(e,h)
  nullify(h) !cleanup
enddo

call jsonc%add(f,e)
call jsonc%add(root,f) !add f to the root structure

Thanks a lot in advance,
Rudy

@jacobwilliams
Copy link
Owner Author

Hi Rudy,

It looks like you’ll have to create a new object to contain Waypoints. You can’t have arrays of arrays in JSON, but you can have arrays of objects (which is what your objective JSON is showing). Here is the solution:

program test

use json_module
use iso_fortran_env,    only: output_unit

implicit none

integer :: i
integer,parameter :: WayPointsNumber = 3
integer,dimension(WayPointsNumber),parameter :: latWayPoints = 1
integer,dimension(WayPointsNumber),parameter :: lonWayPoints = 2
integer,dimension(WayPointsNumber),parameter :: DelayInHoursWayPoints = 3

type(json_value),pointer :: e,f,g,h,json_AnalysisIssues,root,p
type(json_core) :: jsonc

call jsonc%create_object(root,'')  !create root object

! 3.3 WayPoints
call jsonc%create_array(f,'ValidatorRequests')
call jsonc%create_array(e,'WayPoints')
do i=1,WayPointsNumber
    call jsonc%create_object(h,'')
    call jsonc%add(h,'Lat',latWayPoints(i))
    call jsonc%add(h,'Lon',lonWayPoints(i))
    call jsonc%add(h,'DelayInHours',DelayInHoursWayPoints(i))
    call jsonc%add(e,h)
    nullify(h) !cleanup
enddo

call jsonc%create_object(p,'')  !create object to contain e array
call jsonc%add(p,e)    !add e array to p
call jsonc%add(f,p)    !add p to f array

call jsonc%add(root,f) !add f array to the root structure

call jsonc%print(root,output_unit)

end program test

So, I created an object p to contain the Waypoints array, and the result is this:

{
  "ValidatorRequests": [
    {
      "WayPoints": [
        {
          "Lat": 1,
          "Lon": 2,
          "DelayInHours": 3
        },
        {
          "Lat": 1,
          "Lon": 2,
          "DelayInHours": 3
        },
        {
          "Lat": 1,
          "Lon": 2,
          "DelayInHours": 3
        }
      ]
    }
  ]
}

Presumably, if the ValidatorRequests array has more elements, then after you add p, you can then nullify p and create a new one and add that also, etc.

Also you need to first create the root object.

Also note that your first:

call jsonc%add(root,f) !add f to the root structure
nullify(f) !no longer need this

is not correct, since you haven’t created f yet (that actually causes a crash on my computer). So, just delete those two lines (you create f and add it to 'root' further down.'

@rudy26190
Copy link

Thanks Jacob ! Clear.

@zbeekman
Copy link
Contributor

zbeekman commented Jun 10, 2016

You can’t have arrays of arrays in JSON, but you can have arrays of objects (which is what your objective JSON is showing).

Is this true? My reading of json.org is that it is legal JSON to have arrays of arrays since:

  1. An array is an ordered collection of values.
  2. A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

@jacobwilliams
Copy link
Owner Author

I think you're right, and I was mistaken. I guess I did know this at one point (see #156) but forgot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants