5

Say I have data in a JSON object formatted similar to...

{"data":
       [["X","Y","Z"], 
       ["52","23","10"],
       ["46","65","32"]]
}

So essentially, each row takes the form [X, Y, Z].

What would be the easiest way to then access an entire "column" or vector of data? For example, let's assume the "X", "Y", and "Z" is a header row, and I want to access all of the "X" data.

Do I need to iterate over the entire object somehow to retrieve the first member of each element, which would correspond to the "X" column?

I'd like to do this in JavaScript if possible.

Thanks much for any assistance!

4
  • 1
    Can you add description in which language you want to do this Commented Dec 10, 2014 at 6:43
  • @ツPratikButaniツ yep, sorry about that, added to description! :) Thank you! Commented Dec 10, 2014 at 7:35
  • what is final output? Commented Dec 10, 2014 at 7:59
  • @AndroidNoobie I have edited My Answer for JavaScript Commented Dec 10, 2014 at 8:17

4 Answers 4

2

Try below sample code that is simple and straight forward to access the first column.

First iterate the first row to find the correct column then get the values of that column for next rows.

<script type="text/javascript">
    var obj = {
        "data" : [ 
                   [ "X", "Y", "Z" ], 
                   [ "52", "23", "10" ],
                   [ "46", "65", "32" ] 
                 ]
    };
    var column;
    for (var i = 0, j = obj.data.length; i < j; i++) {
        if (obj.data[0][i] === 'X') {
            column = i;
            break;
        }
    }

    for (var i = 1, j = obj.data.length; i < j; i++) {
        var val = obj.data[i][column];
        console.log(val);
    }
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

I am also just learing JavaScript. :)
Had to make one minor fix. For the second for loop, j = obj.data.length, not obj.data[0].length. obj.data[0].length gives the length of the "variables array", so in this example, 3. obj.data.length gives the length of the data column. Submitted the edit for review. Otherwise, all good -- thanks again!
1

"jsonpath" seems to be a good option too:

that would lead to something like

vector X: /data[1][*][1]
vector Y: /data[1][*][2]
vector Z: /data[1][*][3]

1 Comment

Good hint. Please remember that in Stackoverflow bare links are not as much appreciated as much as links + a snippet of code applied to the question's context. If you want to suggest a link, use a comment instead of an answer.
1

I have run this code for Android You may tried out in JavaScript with this type of logic:

    String json = "{\"data\":[[\"X\",\"Y\",\"Z\"],[\"52\",\"23\",\"10\"],[\"46\",\"65\",\"32\"]]}";
    try {
        JSONObject jsonObj = new JSONObject(json);

        Log.i(TAG, "DatA : " + jsonObj.getJSONArray("data").length());
        JSONArray array = jsonObj.getJSONArray("data");
        for(int i=0; i<array.length(); i++) {
            Log.i(TAG, "Value : " + array.getJSONArray(i).getString(0));
        }

    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

Output in Logcat:

12-10 12:26:32.628: I/Demo(28709): DatA : 3
12-10 12:26:32.629: I/Demo(28709): Value : X
12-10 12:26:32.629: I/Demo(28709): Value : 52
12-10 12:26:32.629: I/Demo(28709): Value : 46

Edited: For JavaScript:

<html>
<body>
<p id="demo"></p>

<script>
var text = '{\"data\":[[\"X\",\"Y\",\"Z\"],[\"52\",\"23\",\"10\"],[\"46\",\"65\",\"32\"]]}';
var out = "";
obj = JSON.parse(text);

arr = obj.data;
console.log(arr);
for(i = 0; i < arr.length; i++) {
        out += arr[i][0] + "<br>";
}

document.getElementById("demo").innerHTML = out;

</script>

</body>
</html>

Comments

0

You should try using something like Gson or Jackson Json Processor to handle the JSON and deserialize it into something you can use.

One you do so, you will be able to do something like:

JsonObject json = new JsonParser().parse("...json string...");
JsonArray data = json.get("data");
JsonArray labels = data.getAsJsonArray(0)
for (int i = 1; i < data.size(); i++) {
    JsonArray points = data.getAsJsonArray(i);
    // do something with:  points.getInt(0) -- x column
    // do something with:  points.getInt(1) -- y column
    // do something with:  points.getInt(2) -- z column
}

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.