-1

I have the following JSON file:

{
   "outer_size":2,
   "inner_size":{
      "length_one":2,
      "length_two":1
   }
}

I will use this info to create a new JSON file, whose dimensions are determined by outer_size, inner_size, length_one and length_two. The structure I want to generate has the following form

[
   {
      "a":[
         {
            "a_one":1
         },
         {
            "a_two":2
         }
      ]
   },
   {
      "b":[
         {
            "b_one":1
         }
      ]
   }
]

This structure contains two "outer" variables a and b because outer_size=2.

a contains two "inner" variables a_one and a_two, while b contains one "inner" variable b_one. This is because inner_size is 2 and 1, respectively.

Question Based on a given outer_size, inner_size, length_one and length_two, what is the best way to generate a JSON structure with those dimensions? Can/should it be done with classes?

Please note the following

  1. The value of outer_size must always be equal to the number of length_XX-specifications (in the above example 2). In case it is 3, we will have to specify length_three too.
  2. The specific values of a_one, a_ two etc... can be whatever for this example. Now my main concern is merely to construct the basic structure.
  3. I'm using Nlohmann's JSON library for reading the initial JSON file.
BillyJean
  • 1,537
  • 1
  • 22
  • 39
  • 1
    `"outer_size":2` seems unneeded, as it is the number of key of `"outer_size":2`... – Jarod42 May 04 '17 at 15:20
  • @Jarod42 Thanks. I'd like `outer_size` to be a variable too, so in case where it is 3 we have to specify `length_three` as well – BillyJean May 04 '17 at 15:22

1 Answers1

0

Without using any JSON library, I have been using this code to produce JSON code "manually".

fputs("[\n",file);
fputs("\t{\n",file);
fputs("\t\t\"a\":[\n" ,file);
fputs("\t\t     {\n",file);
fprintf(file,\t\t\t\"a_one\": \"%s\",\n",functionReturningJSONValue());

Which would print something like that you have asked. I haven't done it fully but I am sure you will understand how it works.
Hoping it helped you a bit.

You can still loop in order to create a certain size of JSON and input values with fprintf.

Badda
  • 1,329
  • 2
  • 15
  • 40