1

i have a JSON structure which looks like this.

"content" : {
    "state" : {
      "Idle" : [
        [
          78.366666667,
          1436955825
        ],
        [
          71.281111111,
          1436955840
        ],
        [
          70.41,
          1436955855
        ],
        [
          74.283333333,
          1436955870
        ],
        [
          76.411111111,
          1436955885
        ]
       ] 
        } }

What i have to do is to parse this value. i have to separate comma separated value into x and y. i have to parse these values and separate them. i am having problem issue parsing them

it should look like this

 "Idle" : [
            {
              "x" : 78.366666667,
              "y" :1436955825
            },
            {
              "x" :71.281111111,
              "y" :1436955840
            },
            {
              "x" :70.41,
              "y" :1436955855
            },
            {
              "x" :74.283333333,
              "y" :1436955870
            },
            {
              "x" :76.411111111,
              "y" :1436955885
            }
           ]
5
  • 3
    Json is well understood by Javascript. You can get a plain javascript object and then transform it as you want. What is the issues you're facing ? What have you already tried ? Commented Jul 16, 2015 at 7:50
  • Any code to show your parsing attempt? Commented Jul 16, 2015 at 7:51
  • 1
    don't you think second one is invalid. it should be in {}. Commented Jul 16, 2015 at 7:52
  • @Jai - that would make it invalid Commented Jul 16, 2015 at 7:56
  • so you have JSON string or plain object? Commented Jul 16, 2015 at 8:29

5 Answers 5

3

Map the ...Idle elements to objects (see MDN for Array.map):

test = {
  content: { 
   state: {
     Idle: [
        [
          78.366666667,
          1436955825
        ],
        [
          71.281111111,
          1436955840
        ],
        [
          70.41,
          1436955855
        ],
        [
          74.283333333,
          1436955870
        ],
        [
          76.411111111,
          1436955885
        ]
       ]
   } 
  } 
};


test.content.state.Idle = test.content.state.Idle.map(
                            function (v) {
                              return { x: v[0], y: v[1] };
                            }
                           );

document.querySelector("#result").textContent = JSON.stringify(test, null, " ");
<pre id="result"></pre>

Sign up to request clarification or add additional context in comments.

1 Comment

Indentation preferences aside, this is the best way to do it. +1 :P
0
var container = {
    "content": {
        "state": {
            "Idle": [
                [
                    78.366666667,
                    1436955825
                ],
                [
                    71.281111111,
                    1436955840
                ],
                [
                    70.41,
                    1436955855
                ],
                [
                    74.283333333,
                    1436955870
                ],
                [
                    76.411111111,
                    1436955885
                ]
            ]
        }
    }
};

var _oldIdle = container.content.state.Idle,
    _newIdle = [];

for (var i = 0; i < _oldIdle.length; i++) {
    _newIdle.push({
        x: _oldIdle[i][0],
        y: _oldIdle[i][1]
    });
}

container.content.state.Idle = _newIdle;

Comments

0

Try this way:

var json = {
  "content": {
    "state": {
      "Idle": [
        [
          78.366666667,
          1436955825
        ],
        [
          71.281111111,
          1436955840
        ],
        [
          70.41,
          1436955855
        ],
        [
          74.283333333,
          1436955870
        ],
        [
          76.411111111,
          1436955885
        ]
      ]
    }
  }
};

var newObj = {},
  arr = [];

$.each(json.content.state.Idle, function(i, item) {
  arr.push({x: item[0], y: item[1]});
});
newObj.idle = arr;
console.log(newObj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

2 Comments

arr.push({ x: item[0], y: item[1] });. Don't overcomplicate it.
Thanks @Cerbrus didn't thought like that. Thanks again.
0

If you have JSON string you can use JSON.parse with reviver function

var json = '{"content":{"state":{"Idle":[[78.366666667,1436955825],[71.281111111,1436955840],[70.41,1436955855],[74.283333333,1436955870],[76.411111111,1436955885]]}}}';
var result = JSON.parse(json, function(k, v) {
  if ((v instanceof Array) && (isFinite(Number(k)))) {
    //if array with coordinates - return object instead
    return {
      x: v[0],
      y: v[1]
    };
  }
  return v;
})
console.log(result);
document.getElementById('r').innerHTML = JSON.stringify(result,null,2)
<pre id='r'></pre>

Comments

-1

With UnderscoreJS is simple:

JSFiddle

var values = {
    "content": {
        "state": {
            "Idle": [
                [
                78.366666667,
                1436955825],
                [
                71.281111111,
                1436955840],
                [
                70.41,
                1436955855],
                [
                74.283333333,
                1436955870],
                [
                76.411111111,
                1436955885]
            ]
        }
    }
};

var newValues = _.map(values.content.state.Idle, function (value, key) {
    return {x: value[0], y: value[1]};
});

console.log(newValues);

6 Comments

You don't need a library to map an array.
I didn't say you need it, I just gave my answer that is correct. Isn't the best one? Well, I can agree, but it's correct. Downvote isn't correct imho.
@Michelem well that can be done using plain js so why bother to use a lib.
Downvote tooltip: "This answer is not useful." I think this answer isn't useful because it requires a user to load a library for something that can just as easily be done in native JS. Besides, users can vote how they wish. I didn't have to explain my downvote...
i am using these values for x and y for Rickshaw graph. but the problem is that the rickshaw is not working as the x has double quotes in it.
|

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.