US and Worldwide: +1 (866) 660-7555
Results 1 to 4 of 4

Thread: Easy JSON Output problem

  1. #1
    Join Date
    May 2008
    Posts
    22

    Default Easy JSON Output problem

    Hi,

    who can I create an easy JSON Output statement? I need a message like
    Code:
    {"name":"mueller";"userId":"0815"}
    When I use the JSON Output Step, I recieve
    Code:
    {"element":[{"name":"mueller"},{"user_id":"0815"}]}
    I put in the step:
    General/Json bloc name -> element
    General/Nr rows in a bloc -> 2
    General/Output Element -> element
    Fields/name -> mueller
    Fields/userId -> 0815

    Is there a easy way to use the step or shut I build the statement via string-functions?

    Thank you!

    Braeu

  2. #2
    Join Date
    Sep 2010
    Posts
    1

    Default same question

    I've got exactly the same problem and would appreciate any help if there's any out there.

    I'm using the latest pdi-4.1.0.stable release.
    Last edited by m0rganic; 12-21-2010 at 05:17 PM.

  3. #3
    Join Date
    May 2008
    Posts
    22

    Default

    OK, here are a 90% solution for the "Modified Java Script Value" Step, the procedure I found in the Internet for serialise JS Objects:

    Code:
    var JSON = JSON || {};
    // implement JSON.stringify serialization  
    JSON.stringify = JSON.stringify || function (obj) {  
        var t = typeof (obj);  
        if (t != "object" || obj === null) {  
            // simple data type  
            if (t == "string") obj = '"'+obj+'"';  
            return String(obj);  
        }  
        else {  
            // recurse array or object  
            var n, v, json = [], arr = (obj && obj.constructor == Array);  
            for (n in obj) {  
                v = obj[n]; t = typeof(v);  
                if (t == "string") v = '"'+v+'"';  
                else if (t == "object" && v !== null) v = JSON.stringify(v);  
                json.push((arr ? "" : '"' + n + '":') + String(v));  
            }  
            return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");  
        }  
    };  
    
    
    var oElement = new Object();
    oElement.name="Mueller";
    oElement.userid="0815";
    
    var element = JSON.stringify(oElement);
    work's perfect and produced
    Code:
    {"name":"mueller";"userId":"0815"}
    When I exchange the constants "Mueller" and "0815" with my Inputfield from the previous step, it does not work!

    Code:
    var JSON = JSON || {};
    // implement JSON.stringify serialization  
    JSON.stringify = JSON.stringify || function (obj) {  
        var t = typeof (obj);  
        if (t != "object" || obj === null) {  
            // simple data type  
            if (t == "string") obj = '"'+obj+'"';  
            return String(obj);  
        }  
        else {  
            // recurse array or object  
            var n, v, json = [], arr = (obj && obj.constructor == Array);  
            for (n in obj) {  
                v = obj[n]; t = typeof(v);  
                if (t == "string") v = '"'+v+'"';  
                else if (t == "object" && v !== null) v = JSON.stringify(v);  
                json.push((arr ? "" : '"' + n + '":') + String(v));  
            }  
            return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");  
        }  
    };  
    
    
    var oElement = new Object();
    oElement.name=Kontoname;
    oElement.userid=userId;
    alert ("Name "+oElement.name+" UserID "+oElement.userid);
    
    var element = JSON.stringify(oElement);
    The Alertbox show me the values, but I recieve

    Code:
    {"name":"";"userId":""}
    Now my question: Why?

    Braeu

  4. #4
    Join Date
    May 2008
    Posts
    22

    Default

    Code:
    ...
    var oElement = new Object();
    oElement.name=Kontoname.toString();
    oElement.userid=userId.toString();
    ...
    solve the problem.

    THX Braeu

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •