Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Looking for a tip to parse a convoluted (imho) JSON #2296

pmuvb started this conversation in General
Discussion options

Hi ya'll,

Looking for someone to give me a hand to parse this JSON string, I've spent hours trying different things but
keep getting errors from simdjson.

Just need a tip in what order things should be done, like define a parser, create a doc from parser, etc. I need to
extract "title" and "date" from each element of an array.

https://pastebin.com/5LEBpmfp

Thanks!

You must be logged in to vote

Replies: 2 comments · 1 reply

Comment options

You did not specify what you tried or the errors you got.

I gave ChatGPT the proposed JSON document and asked it to produce DOM and On-Demand (our two APIs) solutions. I told it to assume that the file was named json_file.json. Here are the results...

  1. DOM
#include <iostream>
#include <vector>
#include <string>
#include "simdjson.h"

int main() {
    simdjson::dom::parser parser;
    simdjson::dom::element doc = parser.load("json_file.json");
    simdjson::dom::array data = doc["data"].get_array();

    for (size_t i = 0; i < data.size(); ++i) {
        auto item = data.at(i);
        std::string_view title = item["title"];
        std::string_view date = item["date"];
        std::cout << "Title: " << title << ", Date: " << date << std::endl;
    }

    return EXIT_SUCCESS;
} 
  1. On-Demand
#include <cstdlib>
#include <iostream>
#include <string>
#include "simdjson.h"

int main() {
    simdjson::ondemand::parser parser;
    simdjson::padded_string json = simdjson::padded_string::load("json_file.json");
    simdjson::ondemand::document doc = parser.iterate(json);
    for (auto data : doc["data"]) {
        std::string_view title = data["title"].get_string();
        std::string_view date = data["date"].get_string();
        std::cout << "Title: " << title << ", Date: " << date << std::endl;
    }
    return EXIT_SUCCESS;
}

I have not verified these solutions but they look correct. Given that you have not specified the difficulties you are encountering, I cannot go any further.

Be aware that On-Demand and DOM are two entirely different APIs. You must pick one or the other.

Please read our documentation. We work hard to carefully document the library. Further, if you are encountering difficulties, please be specific.

You must be logged in to vote
0 replies
Comment options

I did ran it through Gemini but it keeps giving me buggy code. ChatGPT looks better.

I just got it to work though, here is my quick code w/o error checking that's based on one
of the examples with similar JSON:

        simdjson::padded_string json_data = simdjson::padded_string(json_str);
        simdjson::ondemand::parser parser;
        simdjson::ondemand::document doc = parser.iterate(json_data);
        simdjson::ondemand::array test_array = doc.find_field("data").get_array();
        for(simdjson::ondemand::object elem: test_array) {
            std::cout << elem["title"] << std::endl;
            std::cout << elem["date"] << std::endl;
        }

Thanks for getting back to me

You must be logged in to vote
1 reply
@lemire
Comment options

This looks like credible code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.