Skip to content

Multiple results should generate valid JSON output #25153

Closed

Description

@uvsmtid

Based on this stackoverflow question.

Problem

Command:

salt --out json '*' test.ping

Output (for 2 minions):

{
    "monkey": true
}
{
    "elephant": true
}

This output is not valid JSON which cannot contain more than one top-level objects (begins with { and ends with }).

The problem is that instead of using JSON output directly, one has to create a special parser to recognize the beginning and the end of multiple JSON objects, split the objects, feed them one by one to the parser. Or, in short, the output is unusable.

Test

load_json.py:

import sys
import json
data = json.load(sys.stdin)
print str(data)

For example, capturing the output above and feeding it into standard JSON parser in Python does not work:

salt --out json '*' test.ping | python load_json.py
Traceback (most recent call last):
  File "load_json.py", line 4, in <module>
    data = json.load(sys.stdin)
  File "/usr/lib64/python2.7/json/__init__.py", line 290, in load
    **kw)
  File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.7/json/decoder.py", line 369, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 4 column 1 - line 7 column 1 (char 23 - 48)

Versions

 salt --versions
           Salt: 2015.5.2
         Python: 2.7.10 (default, May 27 2015, 18:11:38)
         Jinja2: 2.7.3
       M2Crypto: 0.21.1
 msgpack-python: 0.4.6
   msgpack-pure: Not Installed
       pycrypto: 2.6.1
        libnacl: Not Installed
         PyYAML: 3.11
          ioflo: Not Installed
          PyZMQ: 14.4.1
           RAET: Not Installed
            ZMQ: 4.0.5
           Mako: 1.0.1

Activity

  1. changed the title [-]Incorrect JSON output with multiple results[/-] [+]Multiple results should generate valid JSON output[/+] on Jul 5, 2015
  2. uvsmtid commented on Jul 5, 2015

    @uvsmtid
    ContributorAuthor

    #25154 is a similar issue to make sure all features together provide valid output.
    #14725 is a similar issue specific to salt-cloud.

  3. uvsmtid commented on Jul 5, 2015

    @uvsmtid
    ContributorAuthor

    AFAIK, multiple results are independently generated by multiple minions (and each run of function on specific minion actually produces valid output).

    What could be done on the salt command side (run on the master) is to consolidate data from all minions into internal in-memory object and render it into JSON or any other format after (rather than sending JSON to STDOUT without consolidation). To avoid scalability issues, at least it should appear as if it is done so.

    Or, particularly in this case, there is a hack to wrap individual output into JSON array:

    [
    ...
    ,
    ...
    ,
    ...
    ]
    

    The problem is that, in order to avoid parsing by user, it still can only be done on master at least to inject commas ,. And this will only work for JSON, and will only look proper when results are of the same type (conceptually, array contains objects of the same type, semantic, structure, etc.).

  4. added
    bug
    severity-medium
    Core
    P1
    on Jul 6, 2015
  5. added this to the Approved milestone on Jul 6, 2015
  6. uvsmtid commented on Jul 13, 2015

    @uvsmtid
    ContributorAuthor

    I guess this issue is supposed to be fixed by providing --static option and this explanation in documentation.

    At the moment (2015.5.2) my tests fail due to fixed issue #24776 which I'll try as soon as native Fedora package is updated.

    Related #24363.

  7. uvsmtid commented on Jul 16, 2015

    @uvsmtid
    ContributorAuthor

    The issue can be considered fixed in 2015.5.3.

    Test

    Without --static:

    salt --out json '*' test.ping
    jid: 20150717013306606937
    {
        "projector": true
    }
    {
        "rhel7_minion": true
    }
    {
        "rhel5_minion": "Minion did not return. [No response]"
    }
    

    With --static:

     salt --out json --static '*' test.ping
    {
        "rhel7_minion": true, 
        "projector": true
    }
    

    Results

    ... I think the pattern is swinging in the direction of "minions which didn't return are treated as an error"

  8. rallytime commented on Sep 9, 2015

    @rallytime
    Contributor

    @uvsmtid Thanks very much for this excellent analysis and for the pull request. Since you've stated above that this is considered to be fixed by you in 2015.5.3, I am going to go ahead and close this issue. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CoreP1bugseverity-medium

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions