how to create a list in easyJSON

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

how to create a list in easyJSON

Matt Maier
I'm trying to send a list of strings to an API. Best I can tell it's
interpreting the entire list as one string. Is there a way to send data to
easyJSON such that it returns a JSON  list of strings instead of one
monolithic string?

For example, I've got something like this:

array[one] = 12345,12345,12345
array[two] = 12345,12345

and what I get back from easyJSON is this:

{"one":"12345,12345,12345","two":"12345,12345"}

when what I'm trying to get is more like this

{"one":["12345","12345","12345"],"two":["12345","12345"]}
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: how to create a list in easyJSON

Andre Garzia-3
Try placing a:

  Split tArray[one] by comma

Before converting to json.

Om om
Andre
Ps: typing on the phone, sorry for my brevity

Em 30 de dez de 2016 15:29, "Matt Maier" <[hidden email]> escreveu:

> I'm trying to send a list of strings to an API. Best I can tell it's
> interpreting the entire list as one string. Is there a way to send data to
> easyJSON such that it returns a JSON  list of strings instead of one
> monolithic string?
>
> For example, I've got something like this:
>
> array[one] = 12345,12345,12345
> array[two] = 12345,12345
>
> and what I get back from easyJSON is this:
>
> {"one":"12345,12345,12345","two":"12345,12345"}
>
> when what I'm trying to get is more like this
>
> {"one":["12345","12345","12345"],"two":["12345","12345"]}
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: how to create a list in easyJSON

Matt Maier
   put "12345,12345,12345" into tArray["one"]
   put "12345,12345" into tArray["two"]
   split tArray["one"] by comma
   put jsonfromarray(tArray) into tWhatever

result

{"one":{"3":12345,"1":12345,"2":12345},"two":"12345,12345"}

So looks like that just created an array with a numeric index, which is
preserved in the JSON conversion.

On Fri, Dec 30, 2016 at 10:12 PM, Andre Garzia <[hidden email]>
wrote:

> Try placing a:
>
>   Split tArray[one] by comma
>
> Before converting to json.
>
> Om om
> Andre
> Ps: typing on the phone, sorry for my brevity
>
> Em 30 de dez de 2016 15:29, "Matt Maier" <[hidden email]> escreveu:
>
> > I'm trying to send a list of strings to an API. Best I can tell it's
> > interpreting the entire list as one string. Is there a way to send data
> to
> > easyJSON such that it returns a JSON  list of strings instead of one
> > monolithic string?
> >
> > For example, I've got something like this:
> >
> > array[one] = 12345,12345,12345
> > array[two] = 12345,12345
> >
> > and what I get back from easyJSON is this:
> >
> > {"one":"12345,12345,12345","two":"12345,12345"}
> >
> > when what I'm trying to get is more like this
> >
> > {"one":["12345","12345","12345"],"two":["12345","12345"]}
> > _______________________________________________
> > use-livecode mailing list
> > [hidden email]
> > Please visit this url to subscribe, unsubscribe and manage your
> > subscription preferences:
> > http://lists.runrev.com/mailman/listinfo/use-livecode
> >
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: how to create a list in easyJSON

Richard Gaskin
In reply to this post by Matt Maier
Matt Maier wrote:
...
 > when what I'm trying to get is more like this
 >
 > {"one":["12345","12345","12345"],"two":["12345","12345"]}

Let's look at the other side of that and see if we can find a good
round-trip solution:  What would the string you have above look like in
a LiveCode array?

--
  Richard Gaskin
  Fourth World Systems
  Software Design and Development for the Desktop, Mobile, and the Web
  ____________________________________________________________________
  [hidden email]                http://www.FourthWorld.com

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: how to create a list in easyJSON

pink
This post was updated on .
In reply to this post by Matt Maier
in a nutshell, easyJSON creates what you want by using numeric keys

so:
array[one][1] = 12345
array[one][2] = 12345
array[one][3] = 12345
array[two][1] = 12345
array[two][2] = 12345

would produce the results you are looking for (technically the numbers don't have to be sequential, it just has to be that all of the keys must be numeric)

if the split command didn't work correctly, it might be because it is not seeing the keys as being numeric

also, might I suggest you try either fastJSON or mergJSON as they are more up to date
---
Greg (pink) Miller
mad, pink and dangerous to code
Reply | Threaded
Open this post in threaded view
|

Re: how to create a list in easyJSON

Matt Maier
This code:

   put "12345" into tArray["one"][1]
   put "12345" into tArray["one"][2]
   put "12345" into tArray["one"][3]
   put "12345" into tArray["two"][1]
   put "12345" into tArray["two"][2]
   put jsonfromarray(tArray) into tWhatever

gives me this result:

{"one":{"3":12345,"1":12345,"2":12345},"two":{"1":12345,"2":12345}}

which is a multi-level array, rather than this, which is a single-level
array with lists

{"one":["12345","12345","12345"],"two":["12345","12345"]}

I'm not even sure how to distinguish between the two in Livecode. We parse
strings into lists so often that I'm not aware of a pure list data
structure. Livecode can interpret "12345,12345,12345" as a list if I tell
it to go by item, but it's just a string and that's how easyJSON is
treating it, as one string, not a list of strings.

On Sat, Dec 31, 2016 at 10:08 AM, pink <[hidden email]> wrote:

> in a nutshell, easyJSON creates what you want by using numeric keys
>
> so:
> array[one][1] = 12345
> array[one][2] = 12345
> array[one][3] = 12345
> array[two][1] = 12345
> array[two][2] = 12345
>
> would produce the results you are looking for (technically the numbers
> don't
> have to be sequential, it just has to be that all of the keys must be
> numeric)
>
>
>
> --
> View this message in context: http://runtime-revolution.
> 278305.n4.nabble.com/how-to-create-a-list-in-easyJSON-
> tp4711279p4711289.html
> Sent from the Revolution - User mailing list archive at Nabble.com.
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: how to create a list in easyJSON

Richard Gaskin
Matt Maier wrote:

 > This code:
 >
 >    put "12345" into tArray["one"][1]
 >    put "12345" into tArray["one"][2]
 >    put "12345" into tArray["one"][3]
 >    put "12345" into tArray["two"][1]
 >    put "12345" into tArray["two"][2]
 >    put jsonfromarray(tArray) into tWhatever
 >
 > gives me this result:
 >
 > {"one":{"3":12345,"1":12345,"2":12345},"two":{"1":12345,"2":12345}}
 >
 > which is a multi-level array, rather than this, which is a
 > single-level array with lists
 >
 > {"one":["12345","12345","12345"],"two":["12345","12345"]}
 >
 > I'm not even sure how to distinguish between the two in Livecode. We
 > parse strings into lists so often that I'm not aware of a pure list
 > data structure. Livecode can interpret "12345,12345,12345" as a list
 > if I tell it to go by item, but it's just a string and that's how
 > easyJSON is treating it, as one string, not a list of strings.

I think you hit on the core of the issue:  LiveCode Script does not
(yet) support true lists.

xTalks have traditionally approached that with strings, as you noted,
and with the efficiency and ease of chunk expressions that's not a bad
way to handle things.

But it becomes problematic when trying to move data between systems
using a data structure like JSON designed specifically for JavaScript.

I believe LiveCode Builder supports true lists.

It would be nice of some of the language features in LC Builder that are
also common in other languages could be migrated into LC Script as well.

As an interim workaround, I suppose you could write your own
LCArrayToJSON function that handles the specific use cases you're
looking for, but that's tedious and would need to be written and used
carefully with specific tasks in mind.

--
  Richard Gaskin
  Fourth World Systems
  Software Design and Development for the Desktop, Mobile, and the Web
  ____________________________________________________________________
  [hidden email]                http://www.FourthWorld.com

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: how to create a list in easyJSON

mwieder
In reply to this post by Matt Maier
On 12/30/2016 05:27 PM, Matt Maier wrote:

> I'm trying to send a list of strings to an API. Best I can tell it's
> interpreting the entire list as one string. Is there a way to send data to
> easyJSON such that it returns a JSON  list of strings instead of one
> monolithic string?
>
> For example, I've got something like this:
>
> array[one] = 12345,12345,12345
> array[two] = 12345,12345
>
> and what I get back from easyJSON is this:
>
> {"one":"12345,12345,12345","two":"12345,12345"}
>
> when what I'm trying to get is more like this
>
> {"one":["12345","12345","12345"],"two":["12345","12345"]}

Well, technically this isn't really a "problem"...
The json object you're returning is exactly what you put into the array.
The first is a string of 17 characters, the second of 11 characters.
The commas are just characters in a string, nothing special.

However, as both you and Richard point out, LC's lack of true strings is
limiting.

You might try something like this:

    put 12345,"hello",12345 into tElement
    split tElement by comma
    put tElement into tArray["one"]
    put 12345,12345 into tElement
    split tElement by comma
    put tElement into tArray["two"]
    put jsonFromArray(tArray)

result: {"one":[12345,"hello",12345],"two":[12345,12345]}

Q: Do you really want the numeric values quoted in the json object?

--
  Mark Wieder
  [hidden email]


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
--
 Mark Wieder
 ahsoftware@gmail.com
Reply | Threaded
Open this post in threaded view
|

Re: how to create a list in easyJSON

pink
In reply to this post by Matt Maier
what version of easyJSON are you using? I just copied the library from:
https://github.com/luxlogica/easyjson/blob/master/easyjson.lc

used this code:
   put "12345" into tArray["one"][1]
   put "12345" into tArray["one"][2]
   put "12345" into tArray["one"][3]
   put "12345" into tArray["two"][1]
   put "12345" into tArray["two"][2]
   put jsonfromarray(tArray) into tWhatever
and got:
{"one":[12345,12345,12345],"two":[12345,12345]}


I still recommend fastJSON over easyJSON:
https://github.com/bhall2001/fastjson/blob/master/fastjson.lc
---
Greg (pink) Miller
mad, pink and dangerous to code
Reply | Threaded
Open this post in threaded view
|

Re: how to create a list in easyJSON

Matt Maier
That's a good question. The stack doesn't have a version number in it, at
least not that I found.

On Sun, Jan 1, 2017 at 10:31 AM, pink <[hidden email]> wrote:

> what version of easyJSON are you using? I just copied the library from:
> https://github.com/luxlogica/easyjson/blob/master/easyjson.lc
>
> used this code:
>    put "12345" into tArray["one"][1]
>    put "12345" into tArray["one"][2]
>    put "12345" into tArray["one"][3]
>    put "12345" into tArray["two"][1]
>    put "12345" into tArray["two"][2]
>    put jsonfromarray(tArray) into tWhatever
> and got:
> {"one":[12345,12345,12345],"two":[12345,12345]}
>
>
> I still recommend fastJSON over easyJSON:
> https://github.com/bhall2001/fastjson/blob/master/fastjson.lc
>
>
>
> --
> View this message in context: http://runtime-revolution.
> 278305.n4.nabble.com/how-to-create-a-list-in-easyJSON-
> tp4711279p4711302.html
> Sent from the Revolution - User mailing list archive at Nabble.com.
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: how to create a list in easyJSON

pink
check the script...

there should be a function:  isNumericalArray

this is the function which appears to be either not there, or is not functioning correctly
---
Greg (pink) Miller
mad, pink and dangerous to code
Reply | Threaded
Open this post in threaded view
|

Re: how to create a list in easyJSON

J. Landman Gay
In reply to this post by pink
On 1/1/17 12:31 PM, pink wrote:

> what version of easyJSON are you using? I just copied the library from:
> https://github.com/luxlogica/easyjson/blob/master/easyjson.lc
>
> used this code:
>    put "12345" into tArray["one"][1]
>    put "12345" into tArray["one"][2]
>    put "12345" into tArray["one"][3]
>    put "12345" into tArray["two"][1]
>    put "12345" into tArray["two"][2]
>    put jsonfromarray(tArray) into tWhatever
> and got:
> {"one":[12345,12345,12345],"two":[12345,12345]}
>
>
> I still recommend fastJSON over easyJSON:
> https://github.com/bhall2001/fastjson/blob/master/fastjson.lc

The built-in arrayToJSON function gives the same thing.

--
Jacqueline Landman Gay         |     [hidden email]
HyperActive Software           |     http://www.hyperactivesw.com

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: how to create a list in easyJSON

Bob Sneidar-2
In reply to this post by Matt Maier
Hmmm... all of this may explain why a table in a PDF fillable form breaks the controls out as columns, not records. So when populating an FDF file, my data needs to have each column in it's own variable, or else I have to do nested repeats to place it all correctly.

Bob S



_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: how to create a list in easyJSON

JJS via use-livecode
As a warning for others, fastJSON is not directly interchangeable with
easyJSON.

I'm using a big array that has a couple levels of numeric keys before you
get to the text keys. When fastJSON converts the array to JSON it throws
out the numeric keys and just turns everything into a list.

I don't think that would actually be a problem, except that in my data
structure the [0] key has important metadata in it and when fastJSON
converts back from JSON to an array it doesn't start counting at 0, so all
of the data comes back but it's offset.

At least, this is the newest version of fastJSON from github and some old
version of easyJSON that I've had around for a long time (not sure what
version it is).

On Tue, Jan 3, 2017 at 8:05 AM, Bob Sneidar <[hidden email]>
wrote:

> Hmmm... all of this may explain why a table in a PDF fillable form breaks
> the controls out as columns, not records. So when populating an FDF file,
> my data needs to have each column in it's own variable, or else I have to
> do nested repeats to place it all correctly.
>
> Bob S
>
>
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: how to create a list in easyJSON

JJS via use-livecode
In the most recent version of easyJSON, numeric keyed arrays will turn into
JSON arrays, older versions may vary.

fastJSON does this by default, but it can be overridden by passing true into
the keepNumeric parameter:
arrayToJson(pArrayData, keepNumeric)

for example:
put arrayToJSON(myArray,true) into jsonOutput






--
View this message in context: http://runtime-revolution.278305.n4.nabble.com/how-to-create-a-list-in-easyJSON-tp4711279p4711513.html
Sent from the Revolution - User mailing list archive at Nabble.com.

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: how to create a list in easyJSON

pink
In reply to this post by JJS via use-livecode
In the most recent version of easyJSON, numeric keyed arrays will turn into JSON arrays, older versions may vary.

fastJSON does this by default, but it can be overridden by passing true into the keepNumeric parameter:
arrayToJson(pArrayData, keepNumeric)

for example:
put arrayToJSON(inputArray,true) into jsonOutput

hh via use-livecode wrote
As a warning for others, fastJSON is not directly interchangeable with
easyJSON.

I'm using a big array that has a couple levels of numeric keys before you
get to the text keys. When fastJSON converts the array to JSON it throws
out the numeric keys and just turns everything into a list.

I don't think that would actually be a problem, except that in my data
structure the [0] key has important metadata in it and when fastJSON
converts back from JSON to an array it doesn't start counting at 0, so all
of the data comes back but it's offset.

At least, this is the newest version of fastJSON from github and some old
version of easyJSON that I've had around for a long time (not sure what
version it is).

On Tue, Jan 3, 2017 at 8:05 AM, Bob Sneidar <[hidden email]>
wrote:

> Hmmm... all of this may explain why a table in a PDF fillable form breaks
> the controls out as columns, not records. So when populating an FDF file,
> my data needs to have each column in it's own variable, or else I have to
> do nested repeats to place it all correctly.
>
> Bob S
>
>
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
---
Greg (pink) Miller
mad, pink and dangerous to code