4

I am new to using javascript as well as json. I need to extract certain sections from json for processing the data.

{
  "status": "SUCCESS",
  "status_message": "blah blah blah",
  "pri_tag": [
      {
          "tag_id": 1,
          "name": "Tag1"
      },
      {
          "tag_id": 2,
          "name": "Tag2"
      },
      {
          "tag_id": 3,
          "name": "Tag3"
      },
      {
          "tag_id": 4,
          "name": "Tag4"
      }
  ]
}

From the above json message I need to extract pri_tag section so that the extracted json should look like below:

[
  {name:'Tag1', tag_id:1},
  {name:'Tag2', tag_id:2},
  {name:'Tag3', tag_id:3},
  {name:'Tag4', tag_id:4},
  {name:'Tag5', tag_id:5},
  {name:'Tag6', tag_id:6}
];

How to get this done using javascript? Please help me. Thanks in advance.

Thanks friends. I was able to get this working. Thanks once again.

1
  • 1
    @super Hornet gave you the perfect answer! Is your JSON message already loaded as a JSON Object (if you declared it like you did above in your JS, it is). If you load the message with AJAX, you need to add var json_message = JSON.parse(my_json_message_variable) Commented Mar 25, 2014 at 8:12

5 Answers 5

5

try this:

  var data={
  "status": "SUCCESS",
  "status_message": "blah blah blah",
  "pri_tag": [
  {
      "tag_id": 1,
      "name": "Tag1"
  },
  {
      "tag_id": 2,
      "name": "Tag2"
  },
  {
      "tag_id": 3,
      "name": "Tag3"
  },
  {
      "tag_id": 4,
      "name": "Tag4"
  }
  ]
  };

if you get the data from Ajax request you need to parse it like this:

  var newData=JSON.parse(data).pri_tag;

if not, you don't need to parse that:

  var newData=data.pri_tag;
Sign up to request clarification or add additional context in comments.

2 Comments

Who is Jason? :) No need to parse anything, data is already a JSON object, not a string.
@jaranda I just fixed my answer
1
var data={
  "status": "SUCCESS",
  "status_message": "blah blah blah",
  "pri_tag": [
      {
          "tag_id": 1,
          "name": "Tag1"
      },
      {
          "tag_id": 2,
          "name": "Tag2"
      },
      {
          "tag_id": 3,
          "name": "Tag3"
      },
      {
          "tag_id": 4,
          "name": "Tag4"
      }
  ]
}

use this code

var result=data.pri_tag;
for(var i in result)
{
    console.log(result[i]);
}

Comments

0

Try this

var foo = '{"status": "SUCCESS","pri_tag": [...]}'
var bar = JSON.parse(foo)
bar.pri_tag

Comments

0

Hopefully this helps someone, I have a scenario where retrieving JSON.parse(someJsonObject).someAttribute doesn't work. Alternatively, you can also extract attribute data using JSON.parse(someJsonObject)['someAttribute']:

Comments

-2

Assuming you have a data variable containing the JSON object, result would hold what you expect (without using the same data variable):

var result = [];

for(var i = 0; i < data.pri_tag.length; i++){
    result.push({'name': data.pri_tag[i].name, 'tag_id': data.pri_tag[i].tag_id});
}

console.log(result);

Here you are a working example.

5 Comments

Super hornet's answer is better. You do not have to push anything to a seperate array. The data you need is already packaged nicely. Only takes one lune of "code"
Super hornet's answer was actually wrong. But ok, no need to use any other variable (as long as you don't work further with it).
I didn't need testing for it to be true, but thanks ;). Haha, your answer was clear and very useful. Instead of the answer we are commenting on because it uses more code (which can confuse the OP), and it uses more computing power then needed.
Then I would recommend you to test things first: stackoverflow.com/posts/22628592/revisions I didn't know Jason, neither Json ;)
@jaranda, I corrected my typing mistake immediately!

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.