Hitachi Vantara Pentaho Community Forums
Results 1 to 5 of 5

Thread: Json Output

  1. #1

    Default Json Output - Incorrect number of rows

    I am parsing JSON input and generating rows to write to a text file. I am using Java Script to Parse JSON and generate rows as follows:

    result = '{"mn":1,"mx":1,"p":1,"ps":10,"np":1,"nr":2,"qt":"0.023","r":[{"a":{"c":"sentiment","v":"interesting debate going on re","id":3848290697236},"m":1},{"a":{"c":"sentiment.weakpositivesentiment","v":"interesting","id":7146825580549},"m":1}]}';

    idx = getInputRowMeta().size();
    obj = JSON.parse(result);




    for (field in obj.r) {
    row = createRowCopy(getOutputRowMeta().size());
    row[idx] = obj.r[field].a.c;
    row[idx + 1] = obj.r[field].a.v;
    putRow(row);
    }

    But in the output it is accessing n+1 row and writing a extra row saying value not found. I cannot locate where in the code it is trying to access n+1 row....here is the sample output (text in bold is the extra row):

    interaction_ids|column_name|value|idx
    1e35bda0e2bdaa80e074068c6576|sentiment|interesting debate going on re|
    1e35bda0e2bdaa80e074068c6576|sentiment.weakpositivesentiment|interesting|
    1e35bda0e2bdaa80e074068c6576|org.mozilla.javascript.UniqueTag@26a0fd97: NOT_FOUND|org.mozilla.javascript.UniqueTag@26a0fd97: NOT_FOUND|4
    Last edited by smahankali; 03-28-2014 at 02:28 PM.

  2. #2
    Join Date
    Jun 2012
    Posts
    5,534

    Default

    You really should use the JSON Input step instead of JavaScript.

    Code:
    result = '{"mn":1,"mx":1,"p":1,"ps":10,"np":1,"nr":2,"qt":"0.023","r":[{"a":{"c":"sentiment","v":"interesting debate going on re","id":3848290697236},"m":1},{"a":{"c":"sentiment.weakpositivesentiment","v":"interesting","id":7146825580549},"m":1}]}';
    
    idx = getInputRowMeta().size();
    obj = JSON.parse(result);
    
    for (field in obj.r) {
    row = createRowCopy(getOutputRowMeta().size());
    row[idx] = obj.r[field].a.c;
    row[idx + 1] = obj.r[field].a.v;
    putRow(row);
    }
    This code doesn't account for the implicit putRow() which is executed after your own code.
    There are other errors, too.
    Have a look at the JavaScript sample transformations in your Kettle installation folder.
    So long, and thanks for all the fish.

  3. #3

    Default

    Hello Marabu,

    I did use Json Input step. I have attached the transformation file. I copy pasted the json input for your reference, the java script step gets the json from the JSON input step.
    Attached Files Attached Files

  4. #4
    Join Date
    Jun 2012
    Posts
    5,534

    Default

    You don't need scripting, a JSON Input step can extract the information all by itself.

    Name:  JI.png
Views: 82
Size:  16.1 KB
    So long, and thanks for all the fish.

  5. #5

    Default

    Thanks Marabu, that works much better. I did spend way too much time trying to get JavaScript to work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.