7

is there a "Nice" way to get all the values out of a json object (I don't care about the keys) - just get the values into array, without using a loop ? (lang is Javascript)

2
  • 1
    Why don't you want a loop? Without a loop you can try this Commented Dec 19, 2016 at 11:35
  • Thanks, It's a little early for that - I guess in a few months this will be the right answer :-) Commented Dec 19, 2016 at 11:46

5 Answers 5

21

It depends on how you define "loop".

You can extract the properties with Object.keys and then map them to their values.

… it's still essentially a loop under the hood though.

var json = `{ "foo": 1, "bar": 2, "baz": 3 }`;
var obj = JSON.parse(json);
var values = Object.keys(obj).map(function (key) { return obj[key]; });
console.log(values);

With weaker browser support you could use the values method.

var json = `{ "foo": 1, "bar": 2, "baz": 3 }`;
var obj = JSON.parse(json);
var values = Object.values(obj);
console.log(values);

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

2 Comments

thanks - that's what I was looking for. Obviously a loop will occur under the hood, just wanted the source to look smaller.
Object.values now has 94% browser support: caniuse.com/object-values
5

I think you are looking for Object.values() function, just pass the object to the values method of Object as first param. That's it!

Object.values({something: 'lol'});
> ["lol"]

Comments

4

Recursively extract as text

Yes, this is a loop but the underlying methods you are calling such as Object.values or arr.map are still loops. I found this useful for extracting text out of a json object for full text search in particular and thought it useful as I came here initially needing this but the answers only touched the surface as json is recursive in nature.

function textFromJson(json) {
    if (json === null || json === undefined) {
      return '';
    }
    if (!Array.isArray(json) && !Object.getPrototypeOf(json).isPrototypeOf(Object)) {
      return '' + json;
    }
    const obj = {};
    for (const key of Object.keys(json)) {
        obj[key] = textFromJson(json[key]);
    }
    return Object.values(obj).join(' ');
}

Comments

1

With ES2017 you have Object.values(). You can polyfill it also.

Only you need is transform JSON to JavaScript object and call Object.values(). The result is an array of values.

var obj = JSON.parse(jsonData);
var result = Object.values(obj);

2 Comments

I guess it's not available yet... ?
This proposal is at stage 4. If you use Typescript or Babel you can use it and transpilate your code to ES5 javascript. Otherwise, if you include the polyfill you should able to use in your code.
0

If you pass a function to JSON.parse, it will be called each time a value is parsed:

function get_values(json) {
   let values = []
   JSON.parse(json, (key,value)=>{ values.push(value) })
   return values
}

ex:

get_values(`{"a":1, "b":[true, false], "c":3}`)
// returns a list of:
• 1
• true
• false
• [true, false]
• 3
• {a:1, b:[true, false], c:3}

Note: If you don't consider the full object to be a "value", just remove the last item.

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.