0

The following is the data from the source file:

 {
 "dubbuseqchapter+block@a7a5931f68d0482eaff2b7c9f9684e47": {
    "category": "chapter", 
    "children": [
      "dubbuseqsequential+block@968513c8f0cc4249b7cfc2290ac967dc", 
      "dubbuseqsequential+block@f7f730a478144a74bd127f996d6dc4f5", 
      "dubbuseqsequential+block@91a0d5d7cd9649a3bdf057400e0a1c96", 
      "dubbuseqsequential+block@28b2b171b6734b13af29735796c5ad5a", 
      "dubbuseqsequential+block@192a150c8aab43b9bd236773ba60b414", 
      "dubbuseqsequential+block@26b3464dad42460ea66f9afe89770065"
    ], 
    "metadata": {
      "display_name": "Introduction course orientation"
    }
  }, 


  "dubbuseqchapter+block@b2451e9195c5466db8b66f53ed06c9fd": {
    "category": "chapter", 
    "children": [
      "dubbuseqsequential+block@c95826a16f71405ba58319d23d250fc4", 
      "dubbuseqsequential+block@fe4e3b8b7cdd4fa0b9fe9090223b7125", 
      "dubbuseqsequential+block@44bbdee625dc465ebe725d2126ed0662", 
      "dubbuseqsequential+block@8d4daba07d4443f3b2a0b2506280ee2c", 
      "dubbuseqsequential+block@c68d9d3ba7de45b1b0770085e4f1f286", 
      "dubbuseqsequential+block@ccdca5b2aca94dbdabb3a57a75adf3fa"
    ], 
    "metadata": {
      "display_name": "Module closing section"
    }
    }
    }

The following javascript brings the top key values (i.e dubbuseqchapter+block@a7a5931f68d0482eaff2b7c9f9684e47,dubbuseqchapter+block@b2451e9195c5466db8b66f53ed06c9fd )

Javascript code

    var obj = JSON.parse(jContent);
        var keys = Object.keys(obj);


    for (var i = 0; i < keys.length; i++) {
        var row = createRowCopy(getOutputRowMeta().size());
        var idx = getInputRowMeta().size();


            row[idx++] = keys[i];

        //  Alert (keys.length);

            putRow(row);
        }

However, I am unable to get the values of the keys..(i.e. Category, Children and metadata) in this example.

I have tried Objects.values() but it returns null or object object in the Alert.

0

3 Answers 3

5

keys is an array of strings, each string being a property name.

You get the value for a property name in the usual way:

object[property_name]

i.e.

var value = obj[keys[i]];
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your quick reply. I really appreciate that. But all I got in the Alert box is Object Object...thats it...
@Dubbu — That's because it (the value) is an object and you've implicitly called toString() on it. What did you expect?
Hi Quentin, I dont know much about Javascript. I have picked up the code written by someone and managing to get things work. All I want is from the above json source is along with the Keys which it is populating, I need the values (Category, Children and Metadata). Would that be possible? Thanks
Those aren't values. Those are properties of the object (which is the value of the respective property of the parent object). You access them like any other property.
var obj = JSON.parse(jContent); var keys = Object.keys(obj); for (var i = 0; i < keys.length; i++) { var row = createRowCopy(getOutputRowMeta().size()); var idx = getInputRowMeta().size(); row[idx++] = keys[i]; var value = obj[keys[i]]; Alert (value); putRow(row); } Sorry, if I didnt understand...Thanks for your guidance...Still am getting Object Object as a value
0

this code shows how to navigate into parsed JsonData

var obj = JSON.parse(textJson);
var keys = Object.keys(obj);
console.log(obj[keys[0]].metadata.display_name);

this will print : Introduction course orientation

1 Comment

Hi Kevin, Thanks for your valuable tip. It works fine if I give Alert but I am unable to retrieve the Category, Children and Metada (all the 3 child elements). I am using the following code for the total output. var obj = JSON.parse(jContent); var keys = Object.keys(obj); for (var i = 0; i < keys.length; i++) { var row = createRowCopy(getOutputRowMeta().size()); var idx = getInputRowMeta().size(); row[idx++] = keys[i]; row[idx++] = obj[keys[i]].category; putRow(row); } I could bring upto category but not after that. Please help if poss.Thx
0

Or even this way to retrieve your subProperties

var obj = JSON.parse(textJson);
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++){
  console.log(keys[i]);
  var subKeys = Object.keys(obj[keys[i]]);
  for (var j = 0; j < subKeys.length; j++) console.log(subKeys[j] + " --> " + obj[keys[i]][subKeys[j]]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.