6

I have 'modified_data.json' JSON file which has this structure.

{
"data": [
    [
        {
           "account_id": "  "
           "account_name": "   "
        },
        {
           "account_id": "  "
           "account_name": "   "
        },          
        {
           "account_id": "  "
           "account_name": "   "
        },          
        {
           "account_id": "  "
           "account_name": "   "
        }
     ],
     [
        {
           "account_id": "  "
           "account_name": "   "
        },
        {
           "account_id": "  "
           "account_name": "   "
        },
        {
           "account_id": "  "
           "account_name": "   "
        }
     ],
     [
        {
           "account_id": "  "
           "account_name": "   "
        },
        {
           "account_id": "  "
           "account_name": "   "
        },
        {
           "account_id": "  "
           "account_name": "   "
        }
      ]
   ]
}

I want to get first object's account_name from each array...

Does anyone can give me a solution??

Now I'm using Vue.js to display it, I could get each data with python, but Vue.js is not familiar with me yet... Kindly help me :)

1
  • Hi Yohan, welcome to StackOverflow! Can you please start by adding what you have tried? I want to get first object's account_name from each array sounds more like a Javascript question though. Commented Jan 24, 2019 at 4:45

3 Answers 3

8

Well, you will have to add the code that you have written for Vue

If you are in a vue app, you can do something like this

<script>
      import json from './json/data.json'
      export default{
          data(){
              return{
                  myJson: json
              }
          }
      }
</script>

and if you are writting it inside an html page. you can do it in 2 steps.

1st is to add the file link as a script

<script src="../file.json"></script>

then in the vue script section you can assign it to the data object.

var ele = new Vue({
     el : "#YourElement", 
    data : ObjName
});

"ObjName" is a name of the json object in the file.

ObjName :
{
"data": [
    [
        {
           "account_id": "  "
           "account_name": "   "
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer :) but I don't have ObjName for my json object..I want to get "account_name" of first object in each array. Is there any solution for it?
2

You can use a computed property that would reactively take account_name property of the first object of every array:

const data = {
  "data": [
    [
      {
        "account_id": "11",
        "account_name": "name11"
      },
      {
        "account_id": "12",
        "account_name": "name12"
      },
      {
        "account_id": "13",
        "account_name": "name13"
      },
      {
        "account_id": "14",
        "account_name": "name14"
      }
    ],
    [
      {
        "account_id": "21",
        "account_name": "name21"
      },
      {
        "account_id": "22",
        "account_name": "name22"
      },
      {
        "account_id": "23",
        "account_name": "name23"
      }
    ],
    [
      {
        "account_id": "31",
        "account_name": "name31"
      },
      {
        "account_id": "32",
        "account_name": "name32"
      },
      {
        "account_id": "33",
        "account_name": "name33"
      }
    ]
  ]
}


new Vue({
  el: '#demo',
  data() {
    return {
      data: data.data
    }
  },
  computed: {
    firstAccountNames() {
      return this.data.map(dataSet => dataSet[0].account_name)
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <ul>
    <li v-for="name in firstAccountNames">
      {{ name }}
    </li>
  </ul>
</div>

2 Comments

one more question. If I put my json data into the script, it works..But when I try to read json file(same contents), console says error message always. Firefox : SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data Chrome : Uncaught SyntaxError: Unexpected token m in JSON at position 0 at JSON.parse (<anonymous>) What should I do to load json file successfully?
@YohanChoi Probably your web app build tool (webpack I guess), automatically converts JSON into a JavaScript object. Try to remove manual parsing (JSON.parse) and simply use the imported data as a JavaScript object.
1

I solved it! here is code!

var app = new Vue({
el: '#app',
data: {
    datas: []
},

computed: {
    getAccountNames() {
        return this.datas.map(dataSet => dataSet[0].account_name)
    }
},

mounted() {
    var self = this
    $.getJSON("modified_data.json", function(json_data) {
        self.datas = json_data.data
    })
  }
})

Anyway, another question... what is the difference between "this" and "self" ? "self" is equal to "this" I think but when I just use "this", it has error but "self" works well...anyone can tell me the difference?

1 Comment

Regarding this and self - the context of "this" changes within the callback of $.getJSON, and so "self" is just a custom named variable to capture the reference of "this" to be used later, before it's overridden within the callback context.

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.