Export Arrays to json - Forum

Forum Navigation
You need to log in to create posts and topics.

Export Arrays to json

Hi there.

What should I do if I want to repeat several arrays with this pattern 20 times and get the output of the json file?

[{"type":"type1","title":"title1","link":"link1","image":"image1","pubDate":"date1"},{"type":"type2","title":"title2","link":"link2","image":"image2","pubDate":"date2"}, ... ,{"type":"type20","title":"title20","link":"link20","image":"image20","pubDate":"date20"}]

Then I'll ask my next question.

Thanks

@tilesoft Do you mean something like this?

CreateEmptyArray [myArray]
SetVar [myArray(0)] '{"type":"type1","title":"title1","link":"link1","image":"image1","pubDate":"date1"}'
SetVar [myArray(1)] '{"type":"type2","title":"title2","link":"link2","image":"image2","pubDate":"date2"}'
SetVar [myArray(2)] '{"type":"type3","title":"title3","link":"link3","image":"image3","pubDate":"date3"}'
SetVar [myArray(3)] '{"type":"type4","title":"title4","link":"link4","image":"image4","pubDate":"date4"}'
Loop 0 3 [n]
  ParseJSON [myArray([n])] [myArray([n])]
  ConsoleLog [myArray([n])]
EndLoop

Regards

or alternatively:

CreateEmptyArray [myArray]
SetVar [myArray(0)] {"type":"type1","title":"title1","link":"link1","image":"image1","pubDate":"date1"}
SetVar [myArray(1)] {"type":"type2","title":"title2","link":"link2","image":"image2","pubDate":"date2"}
SetVar [myArray(2)] {"type":"type3","title":"title3","link":"link3","image":"image3","pubDate":"date3"}
SetVar [myArray(3)] {"type":"type4","title":"title4","link":"link4","image":"image4","pubDate":"date4"}
ConsoleLog [myArray]

 

tilesoft has reacted to this post.
tilesoft

Hi sir. At first thank you for solution.

I have another question. When my web page read a json file, How can I count items in this json file?

 

Thanks

@tilesoft it depends on your JSON structure but use the length property.
Check this sample code:

BeginJS
$App.myJson = [{ collection :
  {
    "version" : "1.0",
    "href" : "http://example.org/friends/",

    "links" : [
      {"rel" : "feed", "href" : "http://example.org/friends/rss"}
    ],

    "items" : [
      {
        "href" : "http://example.org/friends/jdoe",
        "data" : [
          {"name" : "full-name", "value" : "J. Doe", "prompt" : "Full Name"},
          {"name" : "email", "value" : "jdoe@example.org", "prompt" : "Email"}
        ],
        "links" : [
          {"rel" : "blog", "href" : "http://examples.org/blogs/jdoe", "prompt" : "Blog"},
          {"rel" : "avatar", "href" : "http://examples.org/images/jdoe", "prompt" : "Avatar", "render" : "image"}
        ]
      },

      {
        "href" : "http://example.org/friends/msmith",
        "data" : [
          {"name" : "full-name", "value" : "M. Smith", "prompt" : "Full Name"},
          {"name" : "email", "value" : "msmith@example.org", "prompt" : "Email"}
        ],
        "links" : [
          {"rel" : "blog", "href" : "http://examples.org/blogs/msmith", "prompt" : "Blog"},
          {"rel" : "avatar", "href" : "http://examples.org/images/msmith", "prompt" : "Avatar", "render" : "image"}
        ]
      },

      {
        "href" : "http://example.org/friends/rwilliams",
        "data" : [
          {"name" : "full-name", "value" : "R. Williams", "prompt" : "Full Name"},
          {"name" : "email", "value" : "rwilliams@example.org", "prompt" : "Email"}
        ],
        "links" : [
          {"rel" : "blog", "href" : "http://examples.org/blogs/rwilliams", "prompt" : "Blog"},
          {"rel" : "avatar", "href" : "http://examples.org/images/rwilliams", "prompt" : "Avatar", "render" : "image"}
        ]
      }
    ],

    "queries" : [
      {"rel" : "search", "href" : "http://example.org/friends/search", "prompt" : "Search",
        "data" : [
          {"name" : "search", "value" : ""}
        ]
      }
    ],

    "template" : {
      "data" : [
        {"name" : "full-name", "value" : "", "prompt" : "Full Name"},
        {"name" : "email", "value" : "", "prompt" : "Email"},
        {"name" : "blog", "value" : "", "prompt" : "Blog"},
        {"name" : "avatar", "value" : "", "prompt" : "Avatar"}

      ]
    }
  }
}]
EndJS
SetVar [num] [myJson(0).collection.items.length]
jsAlert [num]

Regards.

 

tilesoft has reacted to this post.
tilesoft

Thank you @luishp

I have another question. I have a json file and I read it. Now how can I add and item at end of it?

 

@tilesoft think on JSON as an Array of objects. Those objects can contain data or even other Arrays with more objects.
Take a look at this sample code:

CreateEmptyArray [myArray]

CreateEmptyObject [user]
SetVar [user.name] "Peter"
SetVar [user.age] 23
ArrayAddItem [myArray] [user]

CreateEmptyObject [user]
SetVar [user.name] "John"
SetVar [user.age] 34
ArrayAddItem [myArray] [user]

CreateEmptyObject [user]
SetVar [user.name] "Carol"
SetVar [user.age] 42
ArrayAddItem [myArray] [user]

ConsoleLog [myArray]

As you can see here, [myArray] is just JSON data.
I hope it helps.

tilesoft has reacted to this post.
tilesoft