简体   繁体   中英

Converting Blob text to array/JSON object in Javascript

How to convert Blob text(that is received from Oracle DB query) to array or JSON object , so that i can get the required items values?

For ex:

            var connection = new ActiveXObject("ADODB.Connection");          
            var connectionstring = "some conection string";
            connection.Open(connectionstring);
            var rs = new ActiveXObject("ADODB.Recordset");                   
            var queryString="Select COLUMNNAME from SOMETABLE where id=123456"   
            rs.Open(queryString, connection);                
            var val=rs.fields(0).value;

Here in val i got cell blob value as like below

\[(\22transaction_id\22,variant \224937178\22);(\22deal_tracking_id\22,variant \224876812\22);(\22instrument_type\22,variant \22COMM-VT\22);(\22internal_portfolio\22,variant \22MA_STRUCTURED_BUY\22);(\22internal_contact\22,variant \22C19850\22);(\22transaction_status\22,variant \22Validated\22);(\22last_update_user\22,variant \22ENDUR_MGR02\22);(\22last_updated\22,variant 2015-11-25T02\3a32\3a00);(\22commodity\22,variant \22Natural Gas\22);(\22source_system\22,variant \22EndurGO\22);(\22input_date\22,variant 2015-10-06T00\3a00\3a00);(\22last_exercise_date\22,variant 2015-11-27T14\3a00\3a00);(\22product_type\22,variant 103)]

From the above blob i wanted to get the value of "transaction_id" How to convert above value to some Array or JSON format in Javascript??

To get this:

[{
  "transaction_id"     : "4937178",
  "deal_tracking_id"   : "4876812",
  "instrument_type"    : "COMM-VT",
  "internal_portfolio" : "MA_STRUCTURED_BUY",
  "internal_contact"   : "C19850",
  "transaction_status" : "Validated",
  "last_update_user"   : "ENDUR_MGR02",
  "last_updated"       : "2015-11-25T02:32:00",
  "commodity"          : "Natural Gas",
  "source_system"      : "EndurGO",
  "input_date"         : "2015-10-06T00:00:00",
  "last_exercise_date" : "2015-11-27T14:00:00",
  "product_type"       : "103"
}]

You have to:

  • Remove ( val.replace('(','');
  • Remove \\22 val.replace('\\22','');
  • Replace ); with , val.replace(');',',');
  • Replace ,variant with : val.replace(',variant',': ');
  • Replace \\3a with : val.replace('\\3a',':'); (Date format!)
  • Replace [ with [{ val.replace('[','[{');
  • Replace ] with }] val.replace(']','}]');

also to get valid JSON all values should be String type (except Numbers).

Is the value of transaction_id supposed to be the variant number there? As in variant \\224937178\\22); ?

Assuming you're always looking for that variant number and the response you receive back is always formatted the same, you could simply use Regex in your Javascript to extract the ID.

var filter = /(\b[\w]{14}\b)\S,\b[\w]{7}\s\W(\d+)/.exec(val);
if (filter === null) {
  console.log("There was an error, the Regex came up empty.");
} else { 
    if (filter[1] === "transaction_id") {
    var transaction_id = filter[2];
    } else {
      var transaction_id = null;
    }
}

You can run the code snippet below to see how it would work inputting HTML onto a page.

 var val = "\\[(\\22transaction_id\\22,variant \\224937178\\22);(\\22deal_tracking_id\\22,variant \\224876812\\22);(\\22instrument_type\\22,variant \\22COMM-VT\\22);(\\22internal_portfolio\\22,variant \\22MA_STRUCTURED_BUY\\22);(\\22internal_contact\\22,variant \\22C19850\\22);(\\22transaction_status\\22,variant \\22Validated\\22);(\\22last_update_user\\22,variant \\22ENDUR_MGR02\\22);(\\22last_updated\\22,variant 2015-11-25T02\\3a32\\3a00);(\\22commodity\\22,variant \\22Natural Gas\\22);(\\22source_system\\22,variant \\22EndurGO\\22);(\\22input_date\\22,variant 2015-10-06T00\\3a00\\3a00);(\\22last_exercise_date\\22,variant 2015-11-27T14\\3a00\\3a00);(\\22product_type\\22,variant 103)]"; var filter = /(\\b[\\w]{14}\\b)\\S,\\b[\\w]{7}\\s\\W(\\d+)/.exec(val); if (filter === null) { $('#test').html("<p class='error'>Uh oh, the regex came up empty"); } else { var transaction_id = filter[2]; if (filter[1] === "transaction_id") { $('#test').html("<p class='success'>Transaction ID: " + transaction_id + "</p>"); } else { $('#test').html("<p class='error'>Uh oh, there was no <b>transaction_id</b> present</p>"); } } 
 .error { background-color: red; color: #fff; font-weight: bold; } .success { background-color: lightgreen; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="test"> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM