wm: irc-h2o-bot

ref: 2477ebd2f3a93498ead2b8c37344032919dc841d
dir: /Test_JSON.rb/

View raw version
require 'json'

# JSON string to be parsed
#json_string = 'data:{"name": "John", "age": 30, "city": "New York"}'

# Parse the JSON string

#test = json_string.sub('data:', '')

# parsed_json = JSON.parse(test)
#
# # Access the values from the parsed JSON
# name = parsed_json['name']
# age = parsed_json['age']
# city = parsed_json['city']
#
# # Output the values
# puts "Name: #{name}"
# puts "Age: #{age}"
# puts "City: #{city}"


data_string = 'data:{"token":{"id":9856,"text":"Hello","logprob":0.0,"special":false},"generated_text":null,"details":null} data:{"token":{"id":9856,"text":"Hello","logprob":0.0,"special":false},"generated_text":null,"details":null}'

# Split the data_string on whitespace followed by "data:" to separate each JSON string
json_strings = data_string.scan(/data:(\{.*?\})/)

parsed_data = []

json_strings.each do |json_str|
  # Prepend "data:" to the JSON string since it was removed during splitting
  # json_str = "data:" + json_str

  # Parse the JSON string
  parsed_data << JSON.parse(json_str)
end

# Now you have an array of parsed JSON objects
parsed_data.each do |data|
  puts data.inspect
end



data_string = 'data:{"token":{"id":9856,"text":"Hello","logprob":0.0,"special":false},"generated_text":null,"details":null} data:{"token":{"id":9856,"text":"Hello","logprob":0.0,"special":false},"generated_text":null,"details":null}'

m = 'data:{"token":{"id":9856,"text":"Hello","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":12,"text":"!","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":1265,"text":" How","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":418,"text":" can","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":295,"text":" I","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":733,"text":" help","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":299,"text":" you","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":1722,"text":" today","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":42,"text":"?","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":11,"text":"<|endoftext|>","logprob":0.0,"special":true},"generated_text":"Hello! How can I help you today?","details":null}';

results = data_string.scan(/data:(\{.*?\})/)

parsed_data = results.map { |item| JSON.parse(item[0]) }

parsed_data.each do |data|
  puts "Name: #{data['token']['text']}"
  # puts "Age: #{data['age']}"
  # puts "City: #{data['city']}"
end