0

I have a rather complex JSON file (output of elasticsearch engine) which I wish to parse using Vue. I managed to parse the JSON and access different values in it, but could not figure out how to parse an array found within the JSON - any suggestions?

Example JSON:

{
    "hits": [
            {
                    "_index": "people",
                    "_type": "lawyer",
                    "_score": 20.591383,
                    "_source": {
                            "name": "Miller J W",
                            "address": "Harvard Law School",
                            "person_id": 23615,
                            "keywords": [
                                    "Human",
                                    "Person",
                                    "Male"
                            ]
                    },
                    "inner_hits": {
                            "top_hits": {
                                    "hits": {
                                            "total": 7,
                                            "max_score": 20.591383,
                                            "hits": [
                                                    {
                                                            "_index": "contracts",
                                                            "_type": "contract",
                                                            "_id": "45386",
                                                            "_score": 20.591383,
                                                            "_source": {
                                                                    "pub_year": 2013,
                                                                    "keywords": [
                                                                            "Contract",
                                                                            "SPA contract",
                                                                            "legal doc",
                                                                    ]
                                                            }
                                                    },
                                                    {
                                                            "_index": "contracts",
                                                            "_type": "contract",
                                                            "_id": "45387",
                                                            "_score": 19.691383,
                                                            "_source": {
                                                                    "pub_year": 2012,
                                                                    "keywords": [
                                                                            "Contract",
                                                                            "CLA contract",
                                                                            "Pro bono",
                                                                    ]
                                                            }
                                                    }
                                            ]
                                    }
                            }
                    }
            },
            {
                    "pesron #2 etc..."
            }
    ]

This is how I parse the JSON using vue:

<ol>
    <li v-for="person in people">
            {{ person._source.name }} 
            {{ person._source.address }} 
            {{ person._source.address_person_id }} 
            {{ person.inner_hits.top_hits.hits.total }}

    </li>

But how do I parse the "hits" under "top_hits"??

Thanks!!

2
  • It's a valid json? It's starts with [ { { so it's looks corrupted. Commented Mar 19, 2019 at 15:30
  • Thanks, I've corrected the JSON.. Commented Mar 19, 2019 at 15:38

1 Answer 1

1

Start with computed property to simplify input data.

computed: {
    people() {
        let people = []
        this.json.hits.forEach((item) => {
            let hits = item.inner_hits.top_hits.hits
            people.push({
                _source: item._source,
                hits: hits,
            })
        })
        return people
    }
}

Your template should looks like:

<ul>
    <li v-for="person in people">
        {{ person._source.name }}<br>
        {{ person._source.address }}<br>
        {{ person._source.address_person_id }}<br>
        {{ person.hits.total }}<br>
         <ul>
             <li v-for="hit in person.hits.hits">
                 {{ hit._source.pub_year }}
                 [...]
             </li>
         </ul>
     </li>
</ul>

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

1 Comment

Thanks @Daniel ! the second loop did the trick - what is the advantage of using the computed data and not just the JSON itself?

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.