3

I am trying to get the items in the json arranged in an orderly manner. I was able to select the "term" values present in the json, but is it possible to arrange this in the manner I have shown in the expected output part? I have added a jsfiddle link to show where I have reached:

[
    {
        "Link": "http://testLink.com/1",
        "_index": "test",
        "_source": {
            "Author": "SAM",
            "Map": [
                {
                    "Company": [
                        {
                            "Apple_Inc": [
                                {
                                    "count": 1,
                                    "term": "Apple"
                                }
                            ],
                            "sector": "Technology",
                            "term": "Apple Inc",
                            "ticker": "AAPL",
                            "type": "BCap"
                        }
                    ],
                    "count": 1,
                    "term": "Company"
                },
                {
                    "Country": [
                        {
                            "Canada": [
                                {
                                    "Canada": [
                                        {
                                            "count": 1,
                                            "term": "Toronto"
                                        }
                                    ],
                                    "count": 1,
                                    "term": "Canada"
                                }
                            ],
                            "United_States": [
                                {
                                    "count": 1,
                                    "term": "United States"
                                }
                            ],
                            "currency": "Dollar (USD)",
                            "index": "DOW JONES INDUS. AVG , S&P 500 INDEX , NASDAQ COMPOSITE INDEX",
                            "region": "North Americas",
                            "term": "Canada"
                        }
                    ],
                    "count": 1,
                    "term": "Country"
                },
                {
                    "Personality": [
                        {
                            "count": 1,
                            "term": "Bart Prince"
                        },
                        {
                            "count": 1,
                            "term": "Thomas"
                        },
                        {
                            "count": 1,
                            "term": "Deborah Hornstra"
                        },
                        {
                            "count": 1,
                            "term": "Henderson Sotheby"
                        },
                        {
                            "count": 1,
                            "term": "Max Alliance"
                        }
                    ],
                    "count": 5,
                    "term": "Personality"
                }
            ]
        },
        "id": "YMFT112"
    },
    {
        "Link": "http://testLink.com/2",
        "_id": "YMFT113",
        "_index": "test",
        "_source": {
            "Author": "MAX",
            "Map": [
                {
                    "Company": [
                        {
                            "Microsoft Corp": [
                                {
                                    "count": 1,
                                    "term": "Microsoft"
                                }
                            ],
                            "sector": "Technology",
                            "term": "Microsoft",
                            "ticker": "AAPL",
                            "type": "BCap"
                        }
                    ],
                    "count": 1,
                    "term": "Company"
                },
                {
                    "Country": [
                        {
                            "Brazil": [
                                {
                                    "count": 1,
                                    "term": "Brazil"
                                }
                            ],
                            "currency": "Dollar (USD)",
                            "region": "South Americas",
                            "term": "Brazil"
                        }
                    ],
                    "count": 1,
                    "term": "Country"
                },
                {
                    "SalesRelated": [
                        {
                            "count": 1,
                            "term": "traffic"
                        }
                    ]
                },
                {
                    "Personality": [
                        {
                            "count": 1,
                            "term": "Maximor"
                        },
                        {
                            "count": 1,
                            "term": "R.V.P"
                        },
                        {
                            "count": 1,
                            "term": "Wenger"
                        },
                        {
                            "count": 1,
                            "term": "SAF"
                        }
                    ],
                    "count": 4,
                    "term": "Personality"
                }
            ]
        }
    }
]

http://jsbin.com/exuwet/3/edit


Prompt Input If field Selected = Country,

Expected Output:

YMFT112;    Country;    United States;  United States;      NA;         http://testLink.com/1;

YMFT112;    Country;    Canada;         Canada;             Toronto;    http://testLink.com/1;

YMFT113;    Country;    Brazil;         Brazil;             NA;         http://testLink.com/2;

If field Selected = Company,

Expected Output:

YMFT112; Company;   Apple Inc;      Apple;      http://testLink.com/1;

YMFT113; Company;   Microsoft Corp; Microsoft;  http://testLink.com/2;
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
user1371896
  • 2,192
  • 7
  • 23
  • 32

3 Answers3

1

You can use the JSON object when natively available or use JSON2 as a shim.

After that it's just a matter of using JavaScript's built in sorting capability. You supply a function that compares to array items against each other

var myArray = JSON.parse(jsonString);
myArray.sort(function(a, b){
    var nameA = a._source.Map.Company.term;
    var nameB = b._source.Map.Company.term;

    if (nameA === nameB) {
        return 0;
    } else if (nameA < nameB) {
        return -1
    }
    return 1;
});
Torsten Walter
  • 5,614
  • 23
  • 26
0

With eval('(' + json_object + ')'), you will be able to create a JavaScript Object. This object will be an array, and you can acess the properties using .. For example, if your json_object is called data, for example: Then

    var temp = eval('(' + data + ')'); // temp now is an array.

if you want to access the first _index or id from the json object:

    "_index": "test",
    "id": "YMFT112",

do alert(temp[0]._index), and it will show you "test". For the other properties, follow the same logic. This stackoverflow question, or the JSON page will help you understand what you have to do in other to have your task accomplished. Yahoo has an API called YUI which may be even more helpful.

Community
  • 1
  • 1
cybertextron
  • 10,547
  • 28
  • 104
  • 208
0

Here is a solution using object-scan

// const objectScan = require('object-scan');

const data = [{"_index":"test","id":"YMFT112","_source":{"Author":"SAM","Map":[{"count":1,"term":"Company","Company":[{"sector":"Technology","ticker":"AAPL","Apple_Inc":[{"count":1,"term":"Apple"}],"term":"Apple Inc","type":"BCap"}]},{"count":1,"term":"Country","Country":[{"region":"North Americas","index":"DOW JONES INDUS. AVG , S&P 500 INDEX , NASDAQ COMPOSITE INDEX","United_States":[{"count":1,"term":"United States"}],"term":"Canada","currency":"Dollar (USD)","Canada":[{"count":1,"term":"Canada","Canada":[{"count":1,"term":"Toronto"}]}]}]},{"count":5,"term":"Personality","Personality":[{"count":1,"term":"Bart Prince"},{"count":1,"term":"Thomas"},{"count":1,"term":"Deborah Hornstra"},{"count":1,"term":"Henderson Sotheby"},{"count":1,"term":"Max Alliance"}]}]},"Link":"http://testLink.com/1"},{"_index":"test","_id":"YMFT113","_source":{"Author":"MAX","Map":[{"count":1,"term":"Company","Company":[{"sector":"Technology","ticker":"AAPL","Microsoft Corp":[{"count":1,"term":"Microsoft"}],"term":"Microsoft","type":"BCap"}]},{"count":1,"term":"Country","Country":[{"region":"South Americas","Brazil":[{"count":1,"term":"Brazil"}],"term":"Brazil","currency":"Dollar (USD)"}]},{"SalesRelated":[{"count":1,"term":"traffic"}]},{"count":4,"term":"Personality","Personality":[{"count":1,"term":"Maximor"},{"count":1,"term":"R.V.P"},{"count":1,"term":"Wenger"},{"count":1,"term":"SAF"}]}]},"Link":"http://testLink.com/2"}];

const find = (term, input) => {
  const r = objectScan([`[*]._source.Map[*].${term}[*].**.term`], {
    reverse: false,
    filterFn: ({ key, parents, context }) => {
      if (Object.values(parents[0]).some((e) => e instanceof Object)) {
        return;
      }
      const root = parents[parents.length - 2];
      context.push([
        root.id || root._id,
        parents[parents.length - 5].term,
        key[key.length - 3].replace(/_/g, ' '),
        ...parents.slice(0, -7).filter((e) => !Array.isArray(e)).map((p) => p.term).reverse(),
        root.Link
      ]);
    }
  })(input, []);
  const maxLength = Math.max(...r.map((e) => e.length));
  r
    .filter((e) => e.length < maxLength)
    .forEach((e) => e.splice(-1, 0, 'NA'.repeat(maxLength - e.length)));
  return r;
};

console.log(find('Country', data).map((e) => e.join(';    ')).join('\n'));
/* =>
YMFT112;    Country;    United States;  United States;      NA;         http://testLink.com/1
YMFT112;    Country;    Canada;         Canada;             Toronto;    http://testLink.com/1
YMFT113;    Country;    Brazil;         Brazil;             NA;         http://testLink.com/2
 */
console.log(find('Company', data).map((e) => e.join(';    ')).join('\n'));
/* =>
YMFT112; Company;   Apple Inc;      Apple;      http://testLink.com/1
YMFT113; Company;   Microsoft Corp; Microsoft;  http://testLink.com/2
 */
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan@13.8.0"></script>

Disclaimer: I'm the author of object-scan

vincent
  • 1,953
  • 3
  • 18
  • 24