Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 12 total
Thread Splitting JSON data between two grids.
Tue, Sep 8 2015 1:44 PMPermanent Link

squiffy

Telemix Ltd.

Avatar

Is there a built in way to cut into JSON data at a given point so I can split an array between two (unbound) grids?
For example, my server returns (conceptually) this :

{
 [
   "inbound" : {
     {   "rows": [
       { "GridColumn1": "Test 1", "GridColumn2": "100", "GridColumn3": "" },
       { "GridColumn1": "Test 2", "GridColumn2": "200", "GridColumn3": "" },
       { "GridColumn1": "Test 3", "GridColumn2": "300", "GridColumn3": "" }
     ] },
   "outbound": {
     { "rows" : [

etc.

and I want to send the "rows" under "inbound" to one grid, and the rows under outbound to another, both on the same page.

I'm sure I can work out how to traverse the returned json data using a TReader but before I bludgeoned my way through that thought I'd ask if there's an obvious trick I'm missing.

Thanks,
Tue, Sep 8 2015 2:11 PMPermanent Link

squiffy

Telemix Ltd.

Avatar

I realise that JSON is full of holes. I think I meant this (an array of rows groups - inbound & outbound in this example) :

[
   {
       "inbound": {
           "rows": [
               {
                   "col": "val"
               },
               {
                   "col2": "val2"
               }
           ]
       }
   },
   {
       "outbound": {
           "rows": [
               {
                   "col": "val"
               },
               {
                   "col2": "val2"
               }
           ]
       }
   }
]
Wed, Sep 9 2015 10:23 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com


<< I'm sure I can work out how to traverse the returned json data using a TReader but before I bludgeoned my way through that thought I'd ask if there's an obvious trick I'm missing. >>

Nope, the trick is to use a TReader instance.  It has neat things like this method:

http://www.elevatesoft.com/manual?action=viewmethod&id=ewb2&comp=TReader&method=SkipProperty

that will allow you to skip completely over any properties that you're not interested in.

Tim Young
Elevate Software
www.elevatesoft.com
Fri, Sep 11 2015 5:57 AMPermanent Link

squiffy

Telemix Ltd.

Avatar

Hi,
please could someone help understand TReader a bit better, as it's really not sinking in & I'm just not getting it. I've followed the example that Tim gave but I can't relate that in my head to what I'm trying to do.

I have the following (contrived) example JSON string, and I want to extract a whole property hierarchy to feed into something else, in this example I want the ROWS array for an unbound TDataset & Grid control.

I create the TReader called Reader.
I initialise Reader with the following JSON from a string :

  someText:='
 [{
   "inbound":{"rows":[
       {"name":"Dave","id":123},
       {"name":"Fred","id",221}
       ]
   },
   "outbound":{"rows":[
       {"name":"Jim","id":889},
       {"name":"Bob","id",786}
       ]
   }
  }]
';

  Reader:=TReader.Create;
  Reader.initialize(someText);

I then start my traversal.
It correctly detects the outer array.
BeginArray moves me to the next character which it correctly identifies as an object.
BeginObject moves me to the property which I can read as "inbound"
SkipPropertySeparator jumps the ":"
Then I have my object.

If this object name is "rows" then I want to take that entire branch and feed it into a dataset for my Grid.
How do I reference it? It's not a string and there's no "ReadObject" type thing.
Do I have to parse it all (ie step through the whole thing) by hand and create my own string, or can I capture that entire branch of the json hierarchy at one fell swoop?

Puzzled of Bath.
Fri, Sep 11 2015 6:54 AMPermanent Link

Chris Holland

SEC Solutions Ltd.

Avatar

Team Elevate Team Elevate

Have you tried the DataSet.LoadRows(rows,true) function.

Not tested it myself but it looks like it may load the data in the rows
object.

Chris Holland
[Team Elevate]

On 11/09/2015 10:57, squiffy wrote:
> Hi,
> please could someone help understand TReader a bit better, as it's really not sinking in & I'm just not getting it. I've followed the example that Tim gave but I can't relate that in my head to what I'm trying to do.
>
> I have the following (contrived) example JSON string, and I want to extract a whole property hierarchy to feed into something else, in this example I want the ROWS array for an unbound TDataset & Grid control.
>
> I create the TReader called Reader.
> I initialise Reader with the following JSON from a string :
>
>     someText:='
>    [{
>      "inbound":{"rows":[
>          {"name":"Dave","id":123},
>          {"name":"Fred","id",221}
>          ]
>      },
>      "outbound":{"rows":[
>          {"name":"Jim","id":889},
>          {"name":"Bob","id",786}
>          ]
>      }
>     }]
> ';
>
>     Reader:=TReader.Create;
>     Reader.initialize(someText);
>
> I then start my traversal.
> It correctly detects the outer array.
> BeginArray moves me to the next character which it correctly identifies as an object.
> BeginObject moves me to the property which I can read as "inbound"
> SkipPropertySeparator jumps the ":"
> Then I have my object.
>
> If this object name is "rows" then I want to take that entire branch and feed it into a dataset for my Grid.
> How do I reference it? It's not a string and there's no "ReadObject" type thing.
> Do I have to parse it all (ie step through the whole thing) by hand and create my own string, or can I capture that entire branch of the json hierarchy at one fell swoop?
>
> Puzzled of Bath.
>
Fri, Sep 11 2015 7:05 AMPermanent Link

squiffy

Telemix Ltd.

Avatar

Hi Chris,
I can load a string that contains just the rows data by doing just that (if my json string started at the "rows" object all is fine), but that's not my issue. My issue is extracting the rows data which is embedded inside other JSON objects.

It looks to me like I have to work my way down manually. Which is fine if that's what I have to do, but it would be so useful to extract an entire branch from a given point that I'm hoping it's built in.
Fri, Sep 11 2015 7:20 AMPermanent Link

Matthew Jones

squiffy wrote:

> Do I have to parse it all (ie step through the whole thing) by hand
> and create my own string, or can I capture that entire branch of the
> json hierarchy at one fell swoop?

I haven't looked into this new-fangled thing yet, but that is exactly
what I did using the old TParser. Things appear to have moved on
though, so I expect there is a better option now...

--

Matthew Jones
Fri, Sep 11 2015 7:30 AMPermanent Link

squiffy

Telemix Ltd.

Avatar

Yeah, I saw your post asking how to step the current position on using TParser, and I guess that's the concept I'm stuck on here. I'm sure it works much better than that, I just don't get it Smile
Fri, Sep 11 2015 8:12 AMPermanent Link

squiffy

Telemix Ltd.

Avatar

For the time being, I'm just doing this. Extremely simplistic, fragile and not very flexible, but it does what I want. I'll maker it better over time ...

function extractRows(s:String):String;
var
  sbcount:integer; // square count.
  cbcount:integer; //curly count   
  tmpStr:String;
  tmpInt:Integer;
  needlePos:Integer;
  currPos:Integer;
  needle:String;
begin            
  needle:=Chr(34)+'inbound'+Chr(34)+':';
  needlePos:=Pos(needle,s);
  if needlePos>0 then
  begin
     // Needle exists.
     currPos:=needlePos+length(needle);
     Result:='';
     sbcount:=0;
     cbcount:=0;
     while currPos <= length(s) do
     begin
        tmpStr:=Copy(s,currPos,1);
        if tmpStr = '[' then Inc(sbcount);
        if tmpStr = '{' then Inc(cbcount);
        if tmpStr = ']' then Dec(sbcount);
        if tmpStr = '}' then Dec(cbcount);
        if tmpStr <>' ' and tmpStr<>#13 then
           Result:=Result + tmpStr;
        if sbcount=0 and cbcount=0 then break;
        Inc(currPos);
     end;
  end;
end;
Fri, Sep 11 2015 9:10 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com


<< If this object name is "rows" then I want to take that entire branch and feed it into a dataset for my Grid.
How do I reference it? It's not a string and there's no "ReadObject" type thing.
Do I have to parse it all (ie step through the whole thing) by hand and create my own string, or can I capture that entire branch of the json hierarchy at one fell swoop? >>

Any way you slice it, you'll need to step through the whole thing.  It's the only way to make sure that you're getting the correct data.

As for getting the actual data, I'm seeing where the issue is with re-constructing the data using TReader.  I'll see if I can add ReadObject/ReadArray methods to do what you want.

Tim Young
Elevate Software
www.elevatesoft.com
Page 1 of 2Next Page »
Jump to Page:  1 2
Image