1

So I want to parse this JSON to a Ruby hash but the JSON has a list type so I don't know how to work with this. Anyone can help me? How can I parse it to Ruby? Thanks

 [
    {
        "name": "unknown script",
        "groups": [
            {
                "name": "unknown group",
                "labels": {},
                "headers": {
                    "sorted": [],
                    "map": {}
                },
                "agent": "",
                "group_type": "",
                "top_comments": [],
                "bottom_comments": [],
                "checks": [
                    {
                        "title": "",
                        "value": "0",
                        "type": "compare"
                    },
                    {
                        "title": "one",
                        "value": "0",
                        "type": "compare"
                    },
                    {
                        "title": "two",

                        "value": "1",
                        "type": "compare"
                    },
                    {
                        "title": "three",

                        "type": "compare"
                    },
                    {
                        "title": "four",

                        "value": "0",
                        "type": "compare"
                    },
                    {
                        "title": "five",

                        "value": "0",
                        "type": "compare"
                    },
                    {
                        "title": "six",

                        "value": "0",
                        "type": "compare"
                    },
                    {
                        "title": "seven",

                        "value": "1",
                        "type": "compare"
                    },
                    {
                        "title": "eight",

                        "value": "1",
                        "type": "compare"
                    },
                    {
                        "title": "nine",

                        "value": "0",
                        "type": "compare"
                    },
                    {
                        "title": "ten",
                        "value": "0",
                        "type": "compare"
                    },
                    {
                        "title": "eleven",
                        "value": "1",
                        "type": "compare"
                    },
                    {
                        "title": "twelve",
                        "value": "1",
                        "type": "compare"
                    },
  
                 
                                ]
                            }
                        ]
                    }
                ]
2
  • This is not a complete or repeatable example. Please update your code with a minimal valid example of your input, and a sample (even if it's hand-crafted) of your expected output. NB: See JSON.parse. Commented Jul 8, 2020 at 19:57
  • This is still invalid input. Per jq: parse error: Expected another array element at line 91, column 33. Commented Jul 9, 2020 at 13:54

1 Answer 1

3

A JSON Array of Objects becomes a Ruby Array of Hashes. These Hashes can then contain more Arrays and so on.

If you want to access the Hashes, you need to iterate through the Array. If you want to access the groups you need to iterate again.

scripts = JSON.parse(json)

# Iterate through each script
scripts.each { |script|
  # Print the script's name.
  puts "Script: #{script["name"]}"

  # Iterate through the script's groups.
  groups = script["groups"]
  groups.each { |group|
    # Print each group's name.
    puts "Group: #{group["name"]}"
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.