Giter VIP home page Giter VIP logo

jsonq's Introduction

jsonQ

jsonQ is a light weight and feature-rich javascript library to make your world around JSON fast and easy.
For a JSON, it makes traversing (you don’t need to make loop inside loop), extracting data and manipulating it very simple.
It also provide some utilty methods , we need while working with JSON which directly works with a JSON .

Installation

For node

npm install jsonq
and include on file using
var jsonQ=require("jsonq");

For browser just include jsonQ.min.js file or jsonQ.js in your directory.

Major Updates

  1. Added UMD Support,
  2. Fixed Major bug #10 (jsonQ method getting overrided if objects have same named keys)

jsonq's People

Contributors

s-yadav avatar ymlsudhanshu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jsonq's Issues

How to do sorting for json array

hi,
I have one small example:
var jsonObj = [{
"age": 30,
"name": "James Martin"
},
{
"age": 44,
"name": "ABC",
}
]

var family = jsonQ(JSON.stringify(jsonObj)),
pnames = family.find('name');

result ==> i am getting same result asc or desc sorting..this is happening only if i have Json array...Could you please suggest how to approach this kind of problem..Basically i have requirement where i need to do sorting based on column in json array..
pnames.sort("name",{order:'ASC'}).value()
["James Martin", "ABC"]
pnames.sort("name",{order:'DESC'}).value()
["James Martin", "ABC"]

find method returning multiple values for single unique key

I have 2 keys at same hierarchy in json like this:
customer{
"customerage": {
"value":"30"
},
"customerageunit":{
"value":"years"
}
}

Now , if I try to find value for customerage, it returns an array having both the values(for customerage and customerageunit). It should return only value for customerage key.

find traversal method not returning as expected. Bug?

Provided we parse the following XML:

<?xml version='1.0' encoding='UTF-8'?>
<mes:Structure xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:mes="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message" xmlns:str="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/structure" xmlns:com="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common" xsi:schemaLocation="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message https://registry.sdmx.org/schemas/v2_1/SDMXMessage.xsd">
    <mes:Header>
        <mes:ID>IDREF93428</mes:ID>
        <mes:Test>false</mes:Test>
        <mes:Prepared>2017-02-16T09:34:05</mes:Prepared>
        <mes:Sender id="ECB" />
        <mes:Receiver id="not_supplied" />
    </mes:Header>
    <mes:Structures>
        <str:OrganisationSchemes>
            <str:AgencyScheme urn="urn:sdmx:org.sdmx.infomodel.base.AgencyScheme=ECB:AGENCIES(1.0)" isExternalReference="false" agencyID="ECB" id="AGENCIES" isFinal="false" version="1.0">
                <com:Name xml:lang="en">Agencies</com:Name>
                <str:Agency urn="urn:sdmx:org.sdmx.infomodel.base.Agency=ECB.DISS" id="DISS">
                    <com:Name xml:lang="en">ECB Dissemination</com:Name>
                </str:Agency>
            </str:AgencyScheme>
            <str:AgencyScheme urn="urn:sdmx:org.sdmx.infomodel.base.AgencyScheme=SDMX:AGENCIES(1.0)" isExternalReference="false" agencyID="SDMX" id="AGENCIES" isFinal="false" version="1.0">
                <com:Name xml:lang="en">SDMX Agency Scheme</com:Name>
                <str:Agency urn="urn:sdmx:org.sdmx.infomodel.base.Agency=SDMX" id="SDMX">
                    <com:Name xml:lang="en">SDMX</com:Name>
                </str:Agency>
                <str:Agency urn="urn:sdmx:org.sdmx.infomodel.base.Agency=ECB" id="ECB">
                    <com:Name xml:lang="en">European Central Bank</com:Name>
                </str:Agency>
                <str:Agency urn="urn:sdmx:org.sdmx.infomodel.base.Agency=EUROSTAT" id="EUROSTAT">
                    <com:Name xml:lang="en">Eurostat</com:Name>
                </str:Agency>
                <str:Agency urn="urn:sdmx:org.sdmx.infomodel.base.Agency=IMF" id="IMF">
                    <com:Name xml:lang="en">International Monetary Fund</com:Name>
                </str:Agency>
                <str:Agency urn="urn:sdmx:org.sdmx.infomodel.base.Agency=ESTAT" id="ESTAT">
                    <com:Name xml:lang="en">Eurostat</com:Name>
                </str:Agency>
            </str:AgencyScheme>
        </str:OrganisationSchemes>
    </mes:Structures>
</mes:Structure>

into a JSON object like this and then use find:

import convert from 'xml-to-json-promise'
import jsonQ from "jsonq"
//data contains the XML
convert.xmlDataToJSON(
  data, 
  {explicitRoot: false, xmlns: false, explicitArray: false}
).then(json => {
  var jqo = jsonQ(json)
  console.log(jqo.find('str:Agency').value())
})

we get the following JSON output:

[ { '$':
     { urn: 'urn:sdmx:org.sdmx.infomodel.base.Agency=ECB.DISS',
       id: 'DISS' },
    'com:Name': { _: 'ECB Dissemination', '$': [Object] } },
  [ { '$': [Object], 'com:Name': [Object] },
    { '$': [Object], 'com:Name': [Object] },
    { '$': [Object], 'com:Name': [Object] },
    { '$': [Object], 'com:Name': [Object] },
    { '$': [Object], 'com:Name': [Object] } ] ]

where elem 1 is an Agency object but elem 2 is an array of Agency objects...
I was expecting the following JSON:

[
 { '$':
     { urn: 'urn:sdmx:org.sdmx.infomodel.base.Agency=ECB.DISS',
       id: 'DISS' },
    'com:Name': { _: 'ECB Dissemination', '$': [Object] } },
 { '$':
     { urn: 'urn:sdmx:org.sdmx.infomodel.base.Agency=SDMX',
       id: 'SDMX' },
    'com:Name': { _: '...', '$': [Object] } },
 { '$':
     { urn: 'urn:sdmx:org.sdmx.infomodel.base.Agency=ECB',
       id: 'ECB' },
    'com:Name': { _: '...', '$': [Object] } },
 { '$':
     { urn: 'urn:sdmx:org.sdmx.infomodel.base.Agency=EUROSTAT',
       id: 'EUROSTAT' },
    'com:Name': { _: '...', '$': [Object] } },
 { '$':
     { urn: 'urn:sdmx:org.sdmx.infomodel.base.Agency=IMF',
       id: 'IMF' },
    'com:Name': { _: '...', '$': [Object] } },
 { '$':
     { urn: 'urn:sdmx:org.sdmx.infomodel.base.Agency=ESTAT',
       id: 'ESTAT' },
    'com:Name': { _: '...', '$': [Object] } }
]

all in one array.
Even if their parents are different, we are looking for all Agency irrespective of the parentage and should hence get an array of Agency and not a mixed array.

What do you think ?

"window is not defined"

Can jsonQ run on a node server? It seems based on this error that it's a client-only script...

W20151112-11:53:14.218(-5)? (STDERR) ReferenceError: window is not defined
W20151112-11:53:14.218(-5)? (STDERR)     at lib/jsonQ.min.js:9:1

Stable sorting

Sorting of similar example json array of objects, from the documentation page, is not stable, and objects become out of order. Can stable sorting aspects be incorporated?

Alternative JSON parsers?

I see in the code that parse is defined as JSON.parse, but that JSON.parse is explicitly used elsewhere. If parse were used everywhere, it would be simple (and very useful) to add "options" support for an alternative JSON parser (i.e. one that handles circular references without throwing an exception). The use or and choice of such a parser would be left completely to the user of jsonQ, by simply passing in a reference to the external parser, replacing the parse: JSON.parse.

Utility method is misspelled : suffle => shuffle

suffle
suffle the element and return a new array of suffled elements.
Syntax : jsonQ.suffle(array)
Return : array - suffled array .
Parameters
array : Array which you want to suffle.

The method should be written as follows: shuffle

value function is missing

Hi.
In my json I have many nodes named value.
When I check the output of the find function and try to call the value() function I noticed that the function is overridden by the a value property of type array.
How can I fix this behaviour?
Thanks in advance.

Sort Entire Array rather than just the found parameter

Rather than having the sort method return only the value of the provided key it would be more useful if there were an option to return the entire JSON object with all data still intact sorted based on the key/parameter identified. Returning an array of sorted values isn't that helpful when it comes to a sort command for most query languages.

ReferenceError: jsonQ is not defined

According to your documentation this should work, but I just get ReferenceError: jsonQ is not defined.
What do I need to add so I can use it?

<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://raw.githubusercontent.com/s-yadav/jsonQ/master/jsonQ.min.js"></script>
</head>
<body>
<script>
alert('script');
$(document).ready(function () {
	alert('ready');
	var jsonObj = { "name": "David"};
	var name = jsonQ(jsonObj).find("name").value();
	alert('working-name='+name);
}
);
</script>
</body>
</html>

Filtering returning an empty set

This may not be an issue with the project but more on how I'm formatting the data. I have search all over and can't find a solution other than to just ask directly.

So, the issue I'm having is that given a Json record that has been compiled through webpack with babel compilation and some manual process I can run .find(), which returns and Array with the teachers json, but can't filter what jsonQ is finding. Below is the code. There is no actual teacher or student data contained within. The Json was created for testing purposes only.

The goal is to have jsonQ to receive data that has been packaged like I have laid out and filter the teachers by grade_level so that I can obtain a list of names of teachers by that grade_level.

Any assistance on how to reformat the data to work with my project would be greatly appreciated.

import jsonQ from 'jsonq'
import students from '../public/json/students.json'
import teachers from '../public/json/teachers.json'

$(() => {
jsonQ.settings.sort.caseIgnore = false
let psData = {
    'students': students,
    'teachers': teachers
}

let data = jsonQ(psData)
let testFind = data.find('teachers')
console.log(testFind.value())
let testFilter = testFind.filter({ grade_level: 3 })
console.log(testFilter.value())
}

Json files that are imported through webpack.

Teachers:

[
    {
        "name": "Mrs. Marry",
        "grade_level": 4
    },
    {
        "name": "Ms. Booth",
        "grade_level": 5
    },
    {
        "name": "Mr. Kenneth",
        "grade_level": 6
    },
    {
        "name": "Mr. Smith",
        "grade_level": 0
    },
    {
        "name": "Mrs. Kemp",
        "grade_level": 1
    },
    {
        "name": "Ms. Jones",
        "grade_level": 2
    },
    {
        "name": "Mr. Dale",
        "grade_level": 3
    }
]

Students:

[
    {
        "firstlast": "Robinson, Billy",
        "dcid": 4321542,
        "grade_level": 0,
        "gender": "male"
    },
    {
        "firstlast": "Anderson, Tommy",
        "dcid": 1234567,
        "grade_level": 1,
        "gender": "male"
    },
    {
        "firstlast": "Jolly, Heather",
        "dcid": 2345678,
        "grade_level": 2,
        "gender": "female"
    },
    {
        "firstlast": "Gibbs, Abby",
        "dcid": 3456789,
        "grade_level": 3,
        "gender": "other"
    },
    {
        "firstlast": "Drake, John",
        "dcid": 4567890,
        "grade_level": 4,
        "gender": "male"
    },
    {
        "firstlast": "Ford, Zoey",
        "dcid": 2345234,
        "grade_level": 5,
        "gender": "female"
    },
    {
        "firstlast": "Peterson, Adrian",
        "dcid": 3245672,
        "grade_level": 6,
        "gender": "other"
    },
    {
        "firstlast": "Bell, Chris",
        "dcid": 6432723,
        "grade_level": 7,
        "gender": "female"
    }
]

Console .find() return with .value()

[Array(7)]
  0: Array(7)
    0: {name: "Mrs. Marry", grade_level: 4}
    1: {name: "Ms. Booth", grade_level: 5}
    2: {name: "Mr. Kenneth", grade_level: 6}
    3: {name: "Mr. Smith", grade_level: 0}
    4: {name: "Mrs. Kemp", grade_level: 1}
    5: {name: "Ms. Jones", grade_level: 2}
    6: {name: "Mr. Dale", grade_level: 3}
    length: 7
    __proto__: Array(0)
    length: 1
  __proto__: Array(0)

case ignoring in .find() method

There no way to ignore casing in .find() method. It will be good if there is a way to ignore the casing while matching key into the JSON

When using jsonq in project getting eval error

jsonQ.js:11 Uncaught (in promise) EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' 'unsafe-inline' https://.oracleiaas.com https://.oraclecloud.com https://.oraclegovcloud.com https://.oraclegovcloud.uk https://.oracle.com: wss://.oraclecloud.com wss://.oc-test.com https://.oc-test.com https://.oracledx.com https://api.dynect.net https://.dynback.net:".

at Function (<anonymous>)
at Object../node_modules/jsonq/jsonQ.js (jsonQ.js:11)
at i (bootstrap:79)
at Object../src/js/app/modules/dataFlow/model/uiModel.js (main.71b848d4ed145ec55e69.js:1)
at i (bootstrap:79)
at Module../src/js/app/modules/dataFlow/dataflowContainer.js (main.71b848d4ed145ec55e69.js:1)
at i (bootstrap:79)
at Object../src/js/app/pluginMain.tsx (pluginMain.tsx:51)
at i (bootstrap:79)
at ociMain.tsx:29

return path based on value

First off, Very nice and clean library! Thumbs up!
I am wondering that the path() only returns based on the key and not on value. It maybe becoz it returns as and when the first occurrence is matched.
Is there any work around so that it returns the path of the key based on its value?

Not a valid json?

Keep getting this error:

/node_modules/jsonq/jsonQ.js:17                                                                                                                                                                                       
            throw msg;                                                                                                                                                                                                                     
                  ^                                                                                                                                                                                                                        
Not a valid json    

Could be just something setup improperly in my workspace - but is there another reason this may happen? Is this just user error?

Find entries that contain a certain string

Hi and first of all thanks for the great work on this library!
I could not figure out yet if there is a way to find entries that contain a certain string.
With the starting letter it works well:
var family = jsonQ(myjson);
var result = family.find('name', function () {
return this[0] == 'f'
});
This will of course return all entries that start with an f. Is there a way to use wildcards or such to query entries that contain a certain string e.g. return this == '%xyz%''
and return for example all entries that are like "foo xyz bar", "barfoo xyz" etc...

Thank you very much in advance!
Stefan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.