bharath
bharath

Reputation: 23

How we will take the keys from a json in the original order?

I have a JSON file, I want to process that JSON data as Key, value pair.

Here is my JSON file

```"users" : {
        "abc": {
            "ip": "-------------",
            "username": "[email protected]",
            "password": "---------",
            "displayname": "-------",
            "Mode": "-----",
            "phonenumber": "1********1",
            "pstndisplay": "+1 *******5"
        },
        "efg": {
            "ip": "-------------",
            "username": "[email protected]",
            "password": "---------",
            "displayname": "-------",
            "Mode": "-----",
            "phonenumber": "1********1",
            "pstndisplay": "+1 *******5"
        },
        "xyz": {
            "ip": "-------------",
            "username": "[email protected]",
            "password": "---------",
            "displayname": "-------",
            "Mode": "-----",
            "phonenumber": "1********1",
            "pstndisplay": "+1 *******5"```

here i tried to get json data

 ``` ${the file as string}=    Get File    ${users_json_path}
  ${parsed}=    Evaluate  json.loads("""${the file as string}""")    json
  ${properties}=  Set Variable  ${parsed["users"]}
  Log  ${properties}
  :FOR    ${key}    IN    @{properties}
  \  ${sub dict}=    Get From Dictionary    ${properties}    ${key}
  \  Log  ${sub dict}
  \  Signin  ${sub dict}[ip]   ${sub dict}[username]   ${sub dict}[password]  ${sub dict}[Mode]
  \  Log  ${key} is successfully signed in.

Expected Behavior - The keys what I am parsing should be in sequence from JSON file. For example, abc will get a sign in first then efg and xyz.

${key} = abc
${key} = efg
${key} = xyz

Below are the questions: 1) How we will take users from JSON in sequence? Right now it is taking randomly 2) What will be the best logic to achieve that?

Upvotes: 2

Views: 281

Answers (2)

Todor Minakov
Todor Minakov

Reputation: 20057

I see you tagged the question with python 2.7 - where Bryan Oakely's comment fully holds true, the elements are in random order.
If you upgrade to python 3 though, starting from v3.6 onwards the dictionaries are guaranteed to preserve the insertion order. Thus on parsing it with the json library the result will be the same as in the source string/file.

Alternatively, in v2 you can use OrderedDict to accomplish the same - plus, specifying the object_pairs_hook argument to JSONDecoder - with it you'll basically specify the result will be OrderedDict:

${parsed}=    Evaluate  json.loads("""${the file as string}""", object_pairs_hook=collections.OrderedDict)    json, collections

Upvotes: 1

mixhowie
mixhowie

Reputation: 36

#!/usr/bin/env python3
import json

def main():
    json_str = """
    {
        "users" : {
            "abc": {
                "ip": "-------------",
                "username": "[email protected]",
                "password": "---------",
                "displayname": "-------",
                "Mode": "-----",
                "phonenumber": "1********1",
                "pstndisplay": "+1 *******5"
            },
            "efg": {
                "ip": "-------------",
                "username": "[email protected]",
                "password": "---------",
                "displayname": "-------",
                "Mode": "-----",
                "phonenumber": "1********1",
                "pstndisplay": "+1 *******5"
            }
        }
    }
    """
    json_object = json.loads(json_str)
    for line in json_str.split('\n'):
        if '"' in line and ':' in line and '{' in line and '"users"' not in line:
            key = line.split('"')[1]
            print(key, json_object['users'][key])

Upvotes: 0

Related Questions