0
var json = {
    "responseHeader": {
        "zkConnected": true,
        "status": 0,
        "QTime": 0
    },
    "spellcheck": {
        "suggestions": ["a", {
            "numFound": 6,
            "startOffset": 0,
            "endOffset": 1,
            "suggestion": ["a", "b", "c", "d", "e", "f"]
        }]
    }
}

How to get suggestion from suggestions in this json using javaScript?

4
  • Show us the full code Commented Sep 21, 2016 at 8:04
  • json.spellcheck.suggestions ? Commented Sep 21, 2016 at 8:05
  • json.spellcheck.suggestions[1].suggestion. JSON is just a combination of arrays and objects. Commented Sep 21, 2016 at 8:07
  • 1
    Sorry but this is not JSON. You code is like var number = 'Red';. Commented Sep 21, 2016 at 8:53

3 Answers 3

1

Like this, if you want to get full array

var allSuggestion = json.spellcheck.suggestions[1].suggestion

or a specific value

var valueA = json.spellcheck.suggestions[1].suggestion[0];

Example Fiddle

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

Comments

0

We can get all the suggestions by using

var Suggestions = json.spellcheck.suggestions[1].suggestion

Or to get a position value you can use the index of it

var Suggestions = json.spellcheck.suggestions[1].suggestion[index]

Also you can apply foreach loop to get the values or objects.

Thanks

Comments

0

enter image description here

Simply looks in above picture for expressive structure of Array in Object.

If structure wrapped by {} that indicates its a Object So you can access property by . which contains property name and its value (any type of value)

If wrapped by [] that indicate it is Array so you can access inner value by [<index>]. it contains set of value(any type of value) only.

Here var json is object because it wrapped by {} inner level property "responseHeader","spellcheck" are object because it's structure wrapped by {}, BUT you look into sub level "suggestions" is wrapped by [] so its array.

If you want to access value of zkConnected so your code should be

console.log(json.responseHeader.zkConnected);

If you want to access value of numFound so your code should be

console.log(json.spellcheck.suggestions[1].numFound);

Because in suggestions is array and have two value one is string "a" at index 0, and another is object at index 1.

If you want to access "e" of suggestion so your code should be

console.log(json.spellcheck.suggestions.suggestion[4]);

Because under suggestions, suggestion is array and e stands on 5th position so you can access it by index 4(position-1)

var json = {
    "responseHeader": {
        "zkConnected": true,
        "status": 0,
        "QTime": 0
    },
    "spellcheck": {
        "suggestions": ["a", {
            "numFound": 6,
            "startOffset": 0,
            "endOffset": 1,
            "suggestion": ["a", "b", "c", "d", "e", "f"]
        }]
    }
}

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.