Question

Back with more questions regarding Mongo and LiveCode, this time with a question specific to MergJSON.

This is the situation: In previous posts I asked some questions on how to connect and get a result from a query, questions that where answered.

Now I have the documents returned form Mongo in a LiveCode field (for verification purposes).

This are the documents returned:

    { "_id" : "001003", "nombre" : "Pedro" }
    { "_id" : "001004", "nombre" : "Alejandro" }
    { "_id" : "001005", "nombre" : "Mario" }
    { "_id" : "001001", "nombre" : "Javier" }
    { "_id" : "001002", "nombre" : "Andrecillo" }

What I want now, and could not make work is this, my stack has a button script I have a mouseup handler:

on mouseUp
    local dbText, dbResultado
    set the hideConsoleWindows to true
    put "var c=db.nueva.find();" into dbText
    put " while(c.hasNext())" after dbText
    put " printjson(c.next())" after dbText
    put shell("C:\mongodb\bin\mongo.exe --eval" && quote & dbText & quote) into dbResultado
    put line 3 to -1  of dbResultado into pJSON
    put  pJSON  into  field "A"  -- just to see what Mongo is returning after eliminating       line 1 and 2 of dbResultado
    put JSONToArray(pJSON) into tArray
    put tArray["nombre"] into fld "B"
end mouseUp

Following this handler are the Decode and Encode functions.

When running this script I get an error:

     executing at 10:15:18 AM
     Type   could not decode JSON: end of file expected near '{'
     Object Mejoraddo
     Line   repeat for each line tKey in mergJSONDecode(pJSON,"tArray")
     Hint   could not decode JSON: end of file expected near '{'

In this line of function JSONToArray pJSON:

     repeat for each line tKey in mergJSONDecode(pJSON,"tArray")

I am almost sure I am missing a simple step or overseeing something obvious. Please, if you need some clarification I'll try to explain this better, thank in advance,

Javier
Was it helpful?

Solution

Not JSON:

{ "_id" : "001003", "nombre" : "Pedro" }
{ "_id" : "001004", "nombre" : "Alejandro" }
{ "_id" : "001005", "nombre" : "Mario" }
{ "_id" : "001001", "nombre" : "Javier" }
{ "_id" : "001002", "nombre" : "Andrecillo" }

JSON:

[
  { "_id" : "001003", "nombre" : "Pedro" },
  { "_id" : "001004", "nombre" : "Alejandro" },
  { "_id" : "001005", "nombre" : "Mario" },
  { "_id" : "001001", "nombre" : "Javier" },
  { "_id" : "001002", "nombre" : "Andrecillo" }
]

See the JSON docs

Try:

local tIndex = 1
repeat for each line tJSON in line 3 to -1  of dbResultado
    put JSONToArray(tJSON) into tArray[tIndex]
    add 1 to tIndex
end repeat
put tArray[1]["nombre"] into fld "B"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top