0

I'm storing a config file in version control (GitLab) which contains information to be read by my ruby app. This info is stored as an object containing objects containing objects.

(Update adding more detail and examples for clarity as requested...)

From within my app I can successfully GET the file (which returns the following JSON Object (some bits trimmed with ... for readability): {"file_name"=>"approval_config.json", "file_path"=>"approval_config.json", "size"=>1331, "encoding"=>"base64", "content_sha256"=>"1c21cbb...fa453fe", "ref"=>"master", "blob_id"=>"de...915", "commit_id"=>"07e...4ff", "last_commit_id"=>"07e...942f", "content"=>"ogICAg...AgICB"}

I can JSON parse the above object and access the contents property on that object. The value of the contents property is a base64Encoded string containing the actual contents of my file in GitLab. I can successfully decode this and see the JSON string stored in GitLab:

"{"G000":{"1":{"max":"4000","name":"Matthew Lewis","id":"ord-matthewl","email":"[email protected]"},"2":{"max":"4000","name":"Brendan Jones","id":"ord-brendanj","email":"[email protected]"},"3":{"max":"20000","name":"Henry Orson","id":"ord-henryo","email":"[email protected]"},"4":{"max":"10000000","name":"Chris Adams","id":"ord-chrisa","email":"[email protected]"}},"G15":{"1":{"max":"4000","name":"Mike Butak","id":"ord-mikebu","email":"[email protected]"},"2":{"max":"4000","name":"Joseph Lister","id":"ord-josephl","email":"[email protected]"},"3":{"max":"20000","name":"Mike Geisler","id":"ord-mikeg","email":"[email protected]"},"4":{"max":"10000000","name":"Samuel Ahn","id":"ord-samuela","email":"[email protected]"}}}"

THIS string (above), I cannot JSON parse. I get an "unexpected token at '{ (JSON::ParserError)" error.

While writing this update it occurs to me that this "un-parsable" string is simply what I put in the file to begin with. Perhaps the method I used to stringify the file's contents in the first place is the issue. I simply pasted a valid javascript object in my browser's console, JSON.stringify'd it, copied the result from the console, and pasted it in my file in GitLab. Perhaps I need to use Ruby's JSON.stringify method to stringify it?

Based on feedback from @ToddA.Jacobs, I tried the following in my ruby script:

require 'rest-client'
require 'json'
require 'base64'
data = RestClient.get 'https://gitlab.companyx.net/api/v4/projects/3895/repository/files/approval_config.json?ref=master', {'PRIVATE-TOKEN':'*********'}
# get the encoded data stored on the 'content' key:
content = JSON.parse(data)['content']
# decode it:
config = Base64.decode64(content)
# print some logs
$evm.log(:info, config)
$evm.log(:info, "config is a Hash? :" + config.is_a?(Hash).to_s) #prints false
$evm.log(:info, "config is a string? :" + config.is_a?(String).to_s) #prints true
hash = JSON.parse(config)
example = hash.dig "G000" "4" "id"
$evm.log(:info, "print exmaple on next line")
$evm.log(:info, example)

That last line prints: The following error occurred during method evaluation: NoMethodError: undefined method 'gsub' for nil:NilClass (drbunix:///tmp/automation_engine20200903-3826-1nbuvl) /usr/local/ lib/ruby/gems/2.5.0/gems/manageiq-password-0.3.0/lib/manageiq/password.rb:89:in 'sanitize_string'

21
  • 1
    Your string is enclosed in "..." but also contains " which doesn't work. Commented Sep 2, 2020 at 14:51
  • 1
    You have invalid quoting of your JSON. Commented Sep 2, 2020 at 15:34
  • 1
    I would have expected all of the embedded double quotes inside the stringified JSON to be escaped: \". Commented Sep 2, 2020 at 16:27
  • 1
    You don't need to use Ruby's JSON stringify. It's an interoperable format so you can generate it from any language. Commented Sep 2, 2020 at 17:09
  • 1
    Use JSON.dump don't write your own JSON. Commented Sep 2, 2020 at 19:22

2 Answers 2

2

Remove Outer Quotes

Your input format is invalid: you're nesting unescaped double quotes, and somehow expecting that to work. Just leave off the outer quotes. For example:

require 'json'

json = <<~'EOF'
    {"G000":{"1":{"max":"4000","name":"Matthew Lewis","id":"ord-matthewl","email":"[email protected]"},"2":{"max":"4000","name":"Brendan Jones","id":"ord-brendanj","email":"[email protected]"},"3":{"max":"20000","name":"Henry Orson","id":"ord-henryo","email":"[email protected]"},"4":{"max":"10000000","name":"Chris Adams","id":"ord-chrisa","email":"[email protected]"}},"G15":{"1":{"max":"4000","name":"Mike Butak","id":"ord-mikebu","email":"[email protected]"},"2":{"max":"4000","name":"Joseph Lister","id":"ord-josephl","email":"[email protected]"},"3":{"max":"20000","name":"Mike Geisler","id":"ord-mikeg","email":"[email protected]"},"4":{"max":"10000000","name":"Samuel Ahn","id":"ord-samuela","email":"[email protected]"}}}
EOF

hash = JSON.parse(json)

hash.dig "G000", "4", "id"
#=> "ord-chrisa"

hash.dig "G15", "4", "id"
#=> "ord-samuela"
Sign up to request clarification or add additional context in comments.

2 Comments

I tried that (see update to post above). Returned an error. Did I misinterpret your guidance?
I tried removing the outer quotes, and I tried using the hash.dig syntax. Not working. A co-worker was able to parse my json without issue.
0

This question was answered by users on another post I opened: Why can Ruby not parse local JSON file?

Ultimately the issue was not Ruby failing to parse my JSON. Rather it was the logging function being unable to log the hash.

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.