How to save data from a JSON formed by arrays?

Asked

Viewed 524 times

0

Hello. I have a JSON that has the following structure:

 {
"_id": {
    "$oid": "3432fa43242"
},
"posts": [{
    "thread": {
        "uuid": "2911da",
        "url": "http://www.google.com",
        "site_full": "opiniaoenoticia.com.br",
        "site": "opiniaoenoticia.com.br",
        "site_section": "http://opiniaoenoticia.com.br/feed/",
        "site_categories": [],
        "section_title": "Opinião e Notícia",
        "title": "Veto a protestos contra Temer na Rio 2016 gera polêmica",
        "title_full": "Veto a protestos contra Temer na Rio 2016 gera polêmica",
        "published": "2016-08-08T18:07:00.000+03:00",
        "replies_count": 0,
        "participants_count": 1,
        "site_type": "news",
        "country": "BR",
        "spam_score": 0.0,
        "main_image": "",
        "performance_score": 0,
        "domain_rank": null,
        "social": {
            "facebook": {
                "likes": 0,
                "comments": 0,
                "shares": 0
            },
            "gplus": {
                "shares": 0
            },
            "pinterest": {
                "shares": 0
            },
            "linkedin": {
                "shares": 0
            },
            "stumbledupon": {
                "shares": 0
            }
        }
    },
    "uuid": "2931da",
    "url": "http://3NXmoxXP9jWxZXABLjFrpB03",
    "ord_in_thread": 0,
    "author": "Melissa",
    "published": "2016-08-08T18:07:00.000+03:00",
    "title": "Veto a protestos contra Temer na Rio 2016 gera polêmica",
    "text": "Início » Brasil » Veto a protestos contra Temer na Rio 2016 gera polêmica.",
    "highlightText": "",
    "highlightTitle": "",
    "language": "portuguese",
    "external_links": [],
    "entities": {
        "persons": [],
        "organizations": [],
        "locations": []
    },
    "crawled": "2016-08-08T22:09:19.938+03:00"
}, {
"thread": {...

Does anyone know how to extract specific data from this array (for example, the data of all the title of each instance of the array) and save in CSV? I’ve tried using Mongodb.

Thank you!

  • The savlar part in CSV is more complicated, but to have the Titles you can do so var titles = json.posts.map(function(post){ return post.title;}); What language do you use on the server?

  • For now I am trying to manipulate the data only in Mongodb. But I can use PHP. The output can be for HTML or TXT.

  • Cannot save exporting this array by mongodb: http://stackoverflow.com/questions/26378769/mongodb-mongoexport-all-objects-in-nested-array http://stackoverflow.com/questions/27017010/mongoexport-csv-output-array-values

1 answer

1

In Mongo you can extract the attributes from within the array like this: db.artigos.find({}, {'posts.thread.title': 1}).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.