0

I have a Json document as:

{
  "_id": "13fee4aad95c7f822c0b559bd8d09fb0",
  "_rev": "5-336dea3680af3e7ec0de29369be90b09",
  "attributeCollection": {
    "attributeArray": [
      {
          "name": "Web Issue",
          "value": [
          "web security authentication"
        ]
      }
    ]
  },
  "hash": "1047fe2e1e58e5c8246b26f015d0ecd7"
}

I already extracted "value" by JS with this code:

if (doc.attributeCollection.attributeArray[i].value) {
        for (var j=0; j<doc.attributeCollection.attributeArray[i].value.length; j++) {
            value = doc.attributeCollection.attributeArray[i].value[j];
        }
}

My problem is how to convert "value" to string. Because "value" just has 1 element, i used some ways as:

  • var content=value.toString();

or

  • var content=value.join("");

Even I use loop as:

var content="";
for(var i; i<value.length; i++){
   content=content+value[i];
}

It still doesn't work, what is the problem in these cases.

3
  • the "value" is extracted successfully from Json, it already returned an array. The problem is how to convert element ""web security authentication" to string. Commented May 3, 2016 at 8:02
  • this value is already in string. why you need to convert again? Commented May 3, 2016 at 8:04
  • It is not a string, because when i call a function with string parameter. It can not recognize "value" is a string. Commented May 3, 2016 at 8:06

3 Answers 3

1

Where is your i?? I have replace i with 0 and its working fine.

  var value=[];
  var doc={
    "_id": "13fee4aad95c7f822c0b559bd8d09fb0",
    "_rev": "5-336dea3680af3e7ec0de29369be90b09",
    "attributeCollection": {
      "attributeArray": [
        {
          "value": [
            "web security authentication"
          ]
        }
      ]
    },
    "hash": "1047fe2e1e58e5c8246b26f015d0ecd7"
  };

  if (doc.attributeCollection.attributeArray[0].value) {
          for (var j=0; j<doc.attributeCollection.attributeArray[0].value.length; j++) {
              value.push(doc.attributeCollection.attributeArray[0].value[j]);
          }
  }

 console.log(value.join(""));
Sign up to request clarification or add additional context in comments.

8 Comments

You printed out an array, not a string.
@NguyenChiHieu I have improve my answer. please check.
the problem which i mentioned is how to convert array "value" to string after extracting from Json. Your solution I already mentioned in the topic. It doesn't work.
do you have any jsfiddle? this is working fine here
as I mentioned alert and console.log printed "value" as array. I checked in a function with string parameter input. It doesn't work with "value".
|
0

Try this:

` var doc={ "_id": "13fee4aad95c7f822c0b559bd8d09fb0", "_rev": "5-336dea3680af3e7ec0de29369be90b09", "attributeCollection": { "attributeArray": [{ "value": [ "web security authentication" ] }] }, "hash": "1047fe2e1e58e5c8246b26f015d0ecd7" };

var value=[];

if(doc.attributeCollection.attributeArray.length>0){
    for(var i=0;i<doc.attributeCollection.attributeArray.length;i++){
        value.push(doc.attributeCollection.attributeArray[i].value);
    }
}

value=value.join("\n");
console.log(value);

`

3 Comments

It still doesn't work, I use a function with string parameter. It can not recognize "value" as a string.
If doc is string -- you can use doc=JSON.parse(doc); and use this code
Yeah, obtaining "value" from Json is already done, I already got "value" but converting the "value" to pure string is my problem.
0

I suspect the problem happens on integer elements. So tried this:

value = value + ""; 

Note: cloudant has trouble indexing empty string. So before calling index on anything, make sure it is never an empty string, or the search will fail on that document without telling you why (index will pass as successful, but it will not work)

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.